0
add_action( 'wp', 'setCookies', 10); 

I am using this hook to set cookies. I am using 'wp' because I need to get the current post ID.

Now on my sidebar.php, I am calling if(isset($_COOKIE['cookiename'])) but it returns nothing.

I checked on chrome devtools and cookiename = postID is exisiting there.

When I refresh the page or go to another page, the cookie is now returning something.

So I'm guessing maybe my cookie is being set only after the sidebar.php is loaded? If so, is there a way around this?

0

The $_COOKIE array contains only these cookies that came in the request from the browser.

setcookie() adds an HTTP header asking the client to send the cookie during subsequent requests.

So yeah - you can’t access $_COOKIE in the same request it is set.

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.