WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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

share|improve this question
$_COOKIE_['region'] should be $_COOKIE['region'] is it a typo? After that: 1) is_page is available from pre_get_posts action; 2) after wp_redirect() always die() or exit. But, all that aside, http://domain.com/uae/ is a page link? It seems so. So when you go to that url, already are in that page, why redirect again? Is possible this is the cause of your endless redirect? – gmazzap Aug 28 '13 at 6:59
Thanks - it was a typo! :( I'll try again. It is a page link, but it only redirects if the cookie is not set to UAE – TomC Aug 28 '13 at 7:04

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 not init because is_page will not work with init

And for the redirect function:

function your_redirect_function_name()
        if(is_page('uae') && $_COOKIE_['region'] !== "UAE"){
add_action( 'template_redirect', 'your_redirect_function_name',9 );

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

share|improve this answer

Your Answer


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.