WordPress cookie problem: Cannot modify header information

Saul Goodman October 29, 2019 Loading...

When you use the setcookie() function in WordPress, there may be a warning like this:

Warning: Cannot modify header information - headers already sent by (output started at a-ugly-path

$_COOKIE still works but the warning is so annoying.


Add <?php ob_start(); ?> into the top of your header.php

<?php ob_start(); ?>
<!DOCTYPE html>
<html lang="en">

    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <?php wp_head(); ?>

And add <?php ob_end_flush(); ?> into the bottom of your footer.php

    <?php wp_footer(); ?>
<?php ob_end_flush(); ?>

Now run your project again and check the result.

Welcome to KINDACODE. Have a nice day and code well!