Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I want to setcookie if is_page('arabic') or is_page('english') then redirect to home_url() only if the page name/slug arabic or english, I think that init action happen before the code determine the current page, how to do that

Also after fixing that problem , how can I be sure that setcookie will happen before the redirect so the cookie which Has just set will be shown (because cookie needs refresh)

Below is my code

function site_set_user_language() {
    if(!isset($_COOKIE['site_language'])){
        setcookie('site_language', 'ar', time()+3600*24*365*3,'/','.site.com');
    }elseif(is_page('arabic') || is_page('english')){
        if($_COOKIE['site_language'] == 'en'){
            setcookie('site_language', 'ar', time()+3600*24*365*3,'/','.site.com');
            wp_redirect( home_url());
            exit();
        }elseif($_COOKIE['site_language'] == 'ar'){
            setcookie('site_language', 'en', time()+3600*24*365*3,'/','.site.com');
            wp_redirect( home_url());
            exit();
        }   
    }   
}
add_action( 'init', 'site_set_user_language',10,1);
add_action( 'template_redirect', 'site_lang_home_redirect' );
share|improve this question
    
got it works : by using wp instead of init, I will send the full code to help anyone has the same issue –  usama sulaiman Sep 10 '13 at 20:48

2 Answers 2

template_redirect and wp (as the OP noted in comments) are good hooks to do this. But there's a problem of logic in the sample code. I think it should be like:

add_action( 'template_redirect', 'cookie_redirect_wpse_113662' );

function cookie_redirect_wpse_113662() {
    # No cookie set, let's do it
    if( !isset( $_COOKIE['site_language'] ) )
        set_cookie_wpse_113662( 'ar' );

    # One of our pages, check/change cookie and redirect
    if( is_page( array( 'arabic', 'english' ) ) ) 
    {
        # Page is Arabic and cookie was English, change cookie
        if( is_page( 'arabic' ) && $_COOKIE['site_language'] == 'en' )
            set_cookie_wpse_113662( 'ar' );

        # Page is English and cookie was Arabic, change cookie
        elseif( is_page( 'english' ) && $_COOKIE['site_language'] == 'ar' ) 
            set_cookie_wpse_113662( 'en' );

        # Redirect 
        do_redirect_wpse_113662();
    }
});
function set_cookie_wpse_113662( $lingo ) {
    setcookie( 'site_language', $lingo, time() + 3600 * 24 * 365 * 3, '/', '.example.com' );
}
function do_redirect_wpse_113662( $where = '' ) {
    $where = empty( $where ) ? home_url() : $where;
    wp_redirect( $where );
    exit();
}

There's another problem, it would be better to go back to the original page when we click on the links Arabic and English: home_url() should be site_url($last_url). See this post, by Konstantin Kovshenin.

For this, at each page, the link for those pages should be http://example.com/LANGUAGE/?original_url=$last_url. But that's a topic for another Question*.

* I've played with the concept and did this plugin.

share|improve this answer
    
Thank you very much, I fixed my problem and sent solution, but I will study your code to understand more ways may it help me in my future WP problems, thanks alot –  usama sulaiman Sep 11 '13 at 6:32
    
I don't need any of add_action('init') or add_action('wp') to make setcookie works? –  usama sulaiman Sep 11 '13 at 7:09
    
@usamasulaiman look at first line add_action('template_redirect'... you can't hook init because when this hook is fired the is_page function doesn't work. In this case brasofilo had choosen the template_redirect action, when conditional tags work, and it is also the best place to do redirect, because when it fires headers are not already sent. –  G. M. Sep 11 '13 at 13:27
    
@G.M. Exactly, I got that I can't use init with is_page, so I decided to use wp instead and it works, as I mentioned in the below answer –  usama sulaiman Sep 11 '13 at 14:17
    
@G.M., yep, not sure if best practice, but it works with the hook wp. &usama-sulaiman, I've optimized the code a bit, but will edit the Answer once more. –  brasofilo Sep 11 '13 at 14:20

Finally I did it, I sent my question after more than a week trying to solve that problem, and it works finally just after 20 minets of sending my question :) I needed to wait 8 hours because I was new to wordpress.stackexchange

So as I mentioned in the comment of my question, The solving ideas were to:

1- Use wp instead of init to make the is_page do its effect.

2- separate the redirect code from the cookie function

So the working code is:

function site_set_user_language() {
    if(!isset($_COOKIE['site_language'])){
        setcookie('site_language', 'ar', time()+3600*24*365*3,'/','.site.com');
    }elseif(is_page('arabic') || is_page('english')){
        if($_COOKIE['site_language'] == 'en'){
            setcookie('site_language', 'ar', time()+3600*24*365*3,'/','.site.com');
        }elseif($_COOKIE['site_language'] == 'ar'){
            setcookie('site_language', 'en', time()+3600*24*365*3,'/','.site.com');
        }   
    }   
}
add_action( 'wp', 'site_set_user_language',10,1);

and the redirect function:

function site_lang_home_redirect()
{
    if(is_page('arabic') || is_page('english')){
        wp_redirect( home_url());
        exit();
    }
}
add_action( 'template_redirect', 'site_lang_home_redirect',9 );
share|improve this answer
    
is_page accepts an array. I think your if($_COOKIE) logic doesn't work. –  brasofilo Sep 11 '13 at 14:30
    
@brasofilo I did it and it works with me exactly like the previous code, Just I can't show you the link of my site now because it is still in the development stage also is_page accepting these parameters $page (mixed) (optional) Page ID, Page Title or Page Slug Default: None codex.wordpress.org/Function_Reference/is_page –  usama sulaiman Sep 11 '13 at 14:37

Your Answer

 
discard

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.