hooks - Cookie cant be read even cookie is present

admin2025-06-02  1

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?

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?

Share Improve this question asked Mar 8, 2019 at 5:50 kydroninkydronin 212 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748831733a314089.html

最新回复(0)