I use cookies to determine which menu I should use on my website.
This is working fine, but I want to force the correct one if someone views the URL directly instead of via the URL with a query string (which gets redirected).
In other words, the URL www.yourdomain.com/?region=UAE gets redirected to www.yourdomain.com/uae/ and I want to be able to show the same result when a new visitor (with no cookies) clicks on a direct link to: www.yourdomain.com/uae/
I can’t use a hook for the init as I’m using the is_page() function.
add_action( 'template_redirect', 'pstv_set_uae_cookie');
function pstv_set_uae_cookie() {
// Set cookie if on UAE Home Page & redirect back
if (is_page('uae') && $_COOKIE_['region'] !== "UAE") {
$expire = time()+60*60*24*30;
setcookie("region", "UAE", $expire, '/', '.domain.com');
wp_redirect( 'http://domain.com/uae/' );
}
}
I’m getting the following error in firefox:
The page isn’t redirecting properly Firefox has detected that the
server is redirecting the request for this address in a way that will
never complete. This problem can sometimes be caused by disabling or
refusing to accept cookies.
I know the redirect needs to happen before anything is output, but I can’t access is_page until the wp functions are initialised.
Can anyone suggest a work-around?
TIA
You need to separate the redirect function from the setcookie function
For setcookie function use
add_action('wp', 'your_cookies_function_name',10,1)
Note that it’s recommended to use
wp
and notinit
becauseis_page
will not work withinit
And for the redirect function:
For more information navigate to this page How to setcookie if is_page(‘page’) ? should I use add_action(‘init’) or there is another action?
I had same problem and I fix it my self