Hi, all.
I'm working on a site where the visitor can dictate which market they're in and, based on that, a part of the navigation looks different. The way that I determine how it looks is by using cookies. My site's at WP Engine and I've worked with them to determine what kind of cookies are best (Javascript) and how to have them exclude the cookie from caching.
The problem is that I do eventually need to get the cookie value into PHP. I've done this and it works, but I find that I often need two loads of the page before the current JS cookie value is available to the PHP cookie code. In looking around for answers, I found someone doing almost exactly the same thing here:
http://esanctuary.net/cookies-with-wordpress-caching/
// Call Ajax
add_action( 'wp_ajax_ajax_action', 'esanctuary_sidebar_cookie'); // ajax for logged in users
add_action( 'wp_ajax_nopriv_ajax_action', 'esanctuary_sidebar_cookie' ); // ajax for not logged in users
I've added something akin to this into my functions.php, but I'm still having the same problem. When I call my function, it's not returning the current cookie value until the second page load. This is my code:
function get_market_cookie() {
// Check if a cookie has been set
if ( isset($_COOKIE["market"]) ) {
// return cookie value
return $_COOKIE["market"];
} else {
return "";
}
}
// Call Ajax
add_action( 'wp_ajax_get_market_cookie', 'get_market_cookie' ); // ajax for logged in users
add_action( 'wp_ajax_nopriv_get_market_cookie', 'get_market_cookie' ); // ajax for not logged in users
I'm sure that my JS cookie code is working, so it seems like this is where the issue must be. I'm just not sure what I'm missing.
Has anyone done this before? Wondering if I'm missing something obvious....
Thanks!