0

I want to redirect WordPress pages as per location e.g domainname/ar, domainname/fr etc, but the code goes into an infinity loop.

Here is the snippest:

function redirect_location(){

    //$UserDetailss =  var_export(unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip=')));
    $UserDetails = unserialize(file_get_contents('http://www.geoplugin.net/php.gp?ip='));
    $userCountry =  $UserDetails['geoplugin_countryCode'];

    if($userCountry == 'AR'){
        $url = home_url('/ar/');
    } else if($userCountry == 'FR'){
        $url = home_url('/fr/');
    } else {
        $url = home_url('/in/');
    }

    if (is_page() || is_home()) {
        wp_redirect($url);
        exit;
    }

}
add_action('template_redirect', 'redirect_location');
2

Us this code instead:

function redirect_location(){

    global $wp;

    $current_url = home_url( $wp->request );

    $UserDetails = unserialize(file_get_contents( 'http://www.geoplugin.net/php.gp?ip=' ) );
    $userCountry =  $UserDetails['geoplugin_countryCode'];

    if($userCountry == 'AR'){
        $url = home_url('/ar/');
    } else if($userCountry == 'FR'){
        $url = home_url('/fr/');
    } else {
        $url = home_url('/in/');
    }

    if( ( is_page() || is_home() ) && ( strpos( $current_url, $url ) === false ) )  {

        wp_redirect($url);
        exit;
    }

}
add_action('template_redirect', 'redirect_location');
| improve this answer | |
  • Okay @Dipendra, Let me check with the code – Shadow Feb 20 at 8:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.