2

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' );
7
  • 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
    Commented Jan 27, 2013 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
    Commented Jan 27, 2013 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
    Commented Jan 27, 2013 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
    Commented Jan 27, 2013 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
    Commented Jan 27, 2013 at 2:06

1 Answer 1

3

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
3
  • 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
    Commented Jan 27, 2013 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
    Commented Jan 27, 2013 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
    Commented Jan 27, 2013 at 4:32

Your Answer

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

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