Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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' );
share|improve this question
    
you don't have to be, it's probably your use of the COOKIEPATH and COOKIE_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. –  Milo Jan 27 '13 at 1:46
    
@Milo I saw these in another example on settings cookies with WordPress. I've updated my question with these constants removed. I still have the same issue though, just can't work out why the cookie is only set after I log in to the admin. –  Andrew Jan 27 '13 at 1:55
    
$_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 to setcookie, it won't exist until the next request. –  Milo Jan 27 '13 at 2:01
    
@Milo I'm aware that the cookie is not set until the next page refresh but that's not the issue I'm facing. I've just tried my code in the theme's functions.php instead of plugin and same issue. –  Andrew Jan 27 '13 at 2:04
    
It's almost like I need to be authenticated before it will allow any cookies to be set. Could this be a server setting? –  Andrew Jan 27 '13 at 2:06
show 2 more comments

1 Answer

It appears I needed to use an earlier hook. Using the wp or init action hooks allow me to read the cookie's value without logging into the admin first.

function output_test_cookie() {

if ( isset( $_COOKIE['test-cookie'] ) )
    echo $_COOKIE["test-cookie"];
}
add_action( 'wp', 'output_test_cookie' ); // wp or init is needed
share|improve this answer
    
It should be noted that this is necessary under whatever strange conditions you've got going on. In a vanilla WP install with twentytwelve it's possible to read cookies anywhere after they're properly set. Using your code to set the cookie, I can read it in the template just fine. –  Milo Jan 27 '13 at 4:07
    
@Milo testing online or locally? I'm also running it from a plugin file, not a template. Everything works perfectly locally for me. –  Andrew Jan 27 '13 at 4:28
    
on a LAMP server, it shouldn't matter if it's online or not. hooks are hooks, whether they're in a plugin or a theme, my point was it should be possible to read the cookie anywhere, from init to wp_footer, there's nothing in WordPress that would prevent this. –  Milo Jan 27 '13 at 4:32
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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