Once logged into the WP admin, I can refresh the front-end and see my cookie's value outputted as "hello world". If I log out of the admin, then refresh my front-end, the cookie's value is now "nothing", as seen in my example function.
This only happens when I'm viewing the website online. When testing locally, I can read the cookie without being logged into the WP admin. Any ideas?
Here's my test function for setting the cookie
function test_cookie() {
setcookie( 'test-cookie', 'hello world', time()+1209600, '/');
}
add_action( 'init', 'test_cookie' );
Here's the function for outputting the cookie's value onto the page
function output_test_cookie() {
if ( isset( $_COOKIE['test-cookie'] ) )
echo $_COOKIE["test-cookie"]; // should output hello world, but only does this when logged into WP admin
else
echo 'nothing';
}
add_action( 'template_redirect', 'output_test_cookie' );
COOKIEPATH
andCOOKIE_DOMAIN
constants that's causing the issue. why are you using them and what are they set to? you normally don't need these unless you have an unusual configuration.$_COOKIE
is set at the beginning of a request, if you're trying to access the value in$_COOKIE
in the same request as the call tosetcookie
, it won't exist until the next request.