I put simple php condition in the header.php of template as this :

setcookie("test_time_".$cookie_id."", $cookies_times, time()+3600);

The result always as this headers already sent, I use this inside plugin and also I try outside the plugin and the result always the same.

It´s possible use cookies in header or what can i do for fix this problem , I need use cookies for works little function.


The issue you have appears because you set your cookies too late. You have to set it before anything will be rendered. Set your cookies in the your-plugin-file.php file.

Also you can check if headers were sent or not by calling headers_sent() function:

add_action( 'init', 'wpse8170_init' );
function wpse8170_init() {
    if ( !headers_sent() ) {
        setcookie("test_time_".$cookie_id."", $cookies_times, time()+3600);
  • The probem i use plugin and i don´t know how i can do this , how i can include this from plugin in header ? , thak´s – user2501504 Oct 10 '13 at 15:21
  • I have updated my answer, check it now – Eugene Manuilov Oct 10 '13 at 15:23
  • If you think that the answer is correct, then mark it as it is. – Eugene Manuilov Oct 10 '13 at 16:55
  • Yes , one thing more please , if i have for example one file in my plugin called code.php how i can insert in header this file from plugin it´s my last question , thank´s regards !!! – user2501504 Oct 10 '13 at 17:07
  • You shouldn't insert it into the header. Just add the snippet into your code.php file and it will add a cookie for you. – Eugene Manuilov Oct 10 '13 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.