Title: Can&#8217;t create PHP cookie: cannot modify headers information
Last modified: April 19, 2021

---

# Can’t create PHP cookie: cannot modify headers information

 *  [sjampie](https://wordpress.org/support/users/sjampie/)
 * (@sjampie)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/cant-create-php-cookie-cannot-modify-headers-information/)
 * 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 5 years, 1 month ago by [sjampie](https://wordpress.org/support/users/sjampie/).

Viewing 3 replies - 1 through 3 (of 3 total)

 *  [RossMitchell](https://wordpress.org/support/users/rossmitchell/)
 * (@rossmitchell)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/cant-create-php-cookie-cannot-modify-headers-information/#post-14335939)
 * 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](https://wordpress.org/support/users/kevin3dynamisch/)
 * (@kevin3dynamisch)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/cant-create-php-cookie-cannot-modify-headers-information/#post-14336174)
 * [@rossmitchell](https://wordpress.org/support/users/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](https://wordpress.org/support/users/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](https://wordpress.org/support/users/bcworkz/)
 * (@bcworkz)
 * [5 years, 1 month ago](https://wordpress.org/support/topic/cant-create-php-cookie-cannot-modify-headers-information/#post-14339692)
 * 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.

## Tags

 * [cookie](https://wordpress.org/support/topic-tag/cookie/)
 * [headers](https://wordpress.org/support/topic-tag/headers/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 3 replies
 * 4 participants
 * Last reply from: [bcworkz](https://wordpress.org/support/users/bcworkz/)
 * Last activity: [5 years, 1 month ago](https://wordpress.org/support/topic/cant-create-php-cookie-cannot-modify-headers-information/#post-14339692)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
