使用geoip时wp_redirect循环

我遇到了一个无限的重定向循环, 当在检测到特定国家/地区时尝试重定向用户时, 导致太多重定向错误。

我要实现的目标是在请求url的末尾添加一个语言参数, 然后重定向到同一页面, 这将导致他们使用自己的语言查看该网站。

这是我所做的:

add_action('template_redirect', 'geoip_redirect');
function geoip_redirect()
{
    if (function_exists('geoip_detect2_get_info_from_current_ip')) {

         $user_info = geoip_detect2_get_info_from_current_ip();
         $country_code = $user_info->country->isoCode;
         $request_url = $_SERVER['REQUEST_URI'].'?lang=he';
         $url = get_site_url(null, $request_url);

         if ($country_code == 'IL') {
             wp_redirect($url);
             exit();
         }

     }
}

#1


你需要在执行代码之前添加一个条件, 例如检查lang参数, 否则它将永远执行下去。

add_action('template_redirect', 'geoip_redirect');
function geoip_redirect()
{
    if (function_exists('geoip_detect2_get_info_from_current_ip')) {
         if ( ! isset( $_GET['lang'] ) ) {
            $user_info = geoip_detect2_get_info_from_current_ip();
            $country_code = $user_info->country->isoCode;
            $request_url = $_SERVER['REQUEST_URI'].'?lang=he';
            $url = get_site_url(null, $request_url);

            if ($country_code == 'IL') {
               wp_redirect($url);
               exit();
            }
         }

     }
}

来源:

https://www.srcmini02.com/66434.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37093.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?