0

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.

1 Answer 1

1

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);
    }
}
7
  • 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 Oct 10, 2013 at 15:21
  • I have updated my answer, check it now Oct 10, 2013 at 15:23
  • If you think that the answer is correct, then mark it as it is. Oct 10, 2013 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 !!! Oct 10, 2013 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. Oct 10, 2013 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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