Support » Fixing WordPress » Get cookie when logged out

  • Resolved incometube

    (@isbuilder)


    Hi,

    I’m setting cookies with JavaScript (due to caching problems) and retrieving them with php in my functions.php.

    Setting the cookie is fine. Retrieving works but only when logged into WordPress using the following. However I need logged out site visitors to get the cookies. Any help would be appreciated. (Please no “echo” examples unless echoing into the value needed for $supercookie below). Thanks.

    add_action( 'init', 'accounturl_shortcode' ); // does this do anything?
    
    add_shortcode('accounturl', 'accounturl_shortcode');
    function accounturl_shortcode( $atts ){
    $output = '';
    $supercookie = '';
    $supercookie = $_COOKIE['cookiename'];
    //have tried this here but doesn't work: if (isset($_COOKIE['cookiename'])){
    $output   = 'http://domain.com/index.php?'.$supercookie.'';
    return $output;
    }
Viewing 5 replies - 1 through 5 (of 5 total)
  • Hey isbuilder,

    Are you using a Managed WordPress host, such as WP Engine, as your hosting provider? Or do you possibly have some other caching system in place? If so, that’s likely the reason you aren’t able to see any cookies in your PHP scripts.

    Thread Starter incometube

    (@isbuilder)

    Yes I’m using cloudways which has around 4 different caching “thingies” running including varnish. I need to try get the value into the php somehow?

    It would be worth reaching out to your host to see how they recommend handling cookies in your PHP scripts. They may have some specific suggestions for how to still accomplish your goal in their environment. Without being familiar with their exact setup, myself and others on these forums will be limited in the advice we can give.

    Thread Starter incometube

    (@isbuilder)

    Yes I’ve just disabled Varnish and it works. I think I’ll just leave it off as have w3 super cache and cdn. Thanks Jeremy!

    Glad to help out!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Get cookie when logged out’ is closed to new replies.