• Good day! I’m developing a plugin where you get to see cookie popup. However, I can’t get this because of a certain error which I constantly get: Warning: Cannot modify header information – headers already sent by (output started at /Applications/MAMP/htdocs/projects/cookie_3dynamisch_plugin/wordpress/wp-includes/class.wp-styles.php:290

    
    wp_enqueue_style('style', '/wp-content/plugins/cc3d/includes/css/style.css');
    
    add_action('init', 'create_cookie');
    
    function create_cookie()
    {
        echo "Hello world!";
        if (!isset($_COOKIE["TestCookie"])) {
            $value = 'something from somewhere';
            setcookie("TestCookie", $value, time() + 3600);
            wp_enqueue_script('script', '/wp-content/plugins/cc3d/includes/js/cookieconsent.js');
        }
    }
    • This topic was modified 3 years, 7 months ago by sjampie.
Viewing 3 replies - 1 through 3 (of 3 total)
  • You can only set cookies BEFORE any page output is output.
    HENCE your “HELLO WORLD!” is truly shooting yourself in the foot. More literately it is amputating your leg at the knee.
    If you need to make sure that your function is getting invoked, do something else like setting an option in the database, or set an value in a global and display it in your page somehow.
    Other causes of the “headers already sent” are theme files with a surplus carriage return after a close PHP phrase.

    Kevin

    (@kevin3dynamisch)

    @rossmitchell ,

    You can see he is actually trying to make it run before any page output is given, see the initilization function (init). Yet the core issue regarding this problem seems to be the how and where he should declare his function.

    The first part of your comment literaly states what he’s asking help with. Making it a global variable however might do the trick, yet that still won’t solve his problem I guess.

    @sjampie ,

    Have you tried calling your cookie as default (no, shown as a 0) in a session and modifying it based on the choice given to a (yes, shown as a 1), which then changes based on you swapping pages. In this case you can just keep your session running and you wouldn’t have to modify your header while loading the page.

    If this doesn’t help, please specify used functions and link your git repo code.

    Moderator bcworkz

    (@bcworkz)

    Doesn’t matter when the code executes if it first echoes out content. Comment out or remove the echo line and you might be able to set cookies.

    While “init” might work for you, the proper action hook from which to send headers is “send_headers”. Funny how that works ??

    Do not enqueue styles when your plugin loads. Instead, for front end styles and scripts, enqueue from the “wp_enqueue_scripts” action hook. Same goes for scripts, they should be enqueued from the same “wp_enqueue_scripts”. While enqueuing from “init” may work, doing so can introduce compatibility issues with other themes or plugins. Best to use the hook intended for the purpose.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Can’t create PHP cookie: cannot modify headers information’ is closed to new replies.