page template - wp_redirect goes to infinity loop

I want to redirect WordPress pages as per location e.g domainnamear, domainnamefr etc, but the code goes into an infin

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('.gp?ip=')));
    $UserDetails = unserialize(file_get_contents('.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');

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/php.gp?ip=')));
    $UserDetails = unserialize(file_get_contents('http://www.geoplugin/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');
Share Improve this question edited Feb 20, 2020 at 9:57 fuxia 107k39 gold badges255 silver badges459 bronze badges asked Feb 20, 2020 at 7:32 ShadowShadow 685 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 2

Us this code instead:

function redirect_location(){

    global $wp;

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

    $UserDetails = unserialize(file_get_contents( 'http://www.geoplugin/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');

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744731573a4590495.html

相关推荐

  • page template - wp_redirect goes to infinity loop

    I want to redirect WordPress pages as per location e.g domainnamear, domainnamefr etc, but the code goes into an infin

    1天前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信