WP_REDIRECT进入无限循环

时间:2020-02-20 作者:Shadow

我想根据位置重定向WordPress页面,例如域名/ar、域名/fr等,但代码进入无限循环。

这是飞贼:

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\');

1 个回复
最合适的回答,由SO网友:Dipendra Pancholi 整理而成

请使用此代码:

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\');

相关推荐