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 have a main homepage for my site and will have several other 'homepages' for different countries in the future. I am trying to redirect the user to the right 'homepage' for their country based on their IP, but also give them the option to view a different region for users stuck behind a proxy in a different country, for example. To do this I am setting 2 cookies; Country (for IP Lookup) and Region (for user's requested/preferred area).

I have seen various posts on here relating to location redirection, but no-one has suggested using PHP's GeoIP tools. Is there any reason why I shouldn't use that method? I know that $_SERVER['REMOTE_ADDR'] isn't the most accurate, but surely it's a quick and easy method?

Here is my current code:

// PSTV Regional Settings
add_action( 'init', 'pstv_set_country');
function pstv_set_country() {
    $country = geoip_country_code3_by_name($_SERVER['REMOTE_ADDR']);
    $expire = time()+60*60*24*30;
    setcookie("country", $country, $expire, '/', '.plainandsimple.tv');

    // Setup redirects for Country Homepages Here
    if ($_COOKIE['country'] == "UAE") {
        wp_redirect( 'http://plainandsimple.tv/uae/' ); 
        exit; 
    } 
    else {
        wp_redirect( home_url() ); 
        exit;
    }
}    

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, '/', '.plainandsimple.tv');
        wp_redirect( 'http://plainandsimple.tv/uae/' ); 
        exit; 
    }
    if (is_page('home') && $_COOKIE['region'] == "UAE") {
        $expire = time()+60*60*24*30;
        setcookie("region", "UAE", $expire, '/', '.plainandsimple.tv');
        wp_redirect( 'http://plainandsimple.tv/uae/' ); 
        exit; 
    }
}

So the question is, does anyone see any issue in my approach to this or can someone give me and pointers to a better method?

share|improve this question
add comment

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.