我已经建立了这个功能,允许我根据用户的国家/地区重定向用户,它似乎适用于除管理部分以外的所有页面。显然,如果我在管理部分,我不想重定向,但目前我得到了一个重定向循环和一个错误。
这是我的职责:
function url_get_contents ($Url) {
if (!function_exists(\'curl_init\')){
die(\'CURL is not installed!\');
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
curl_close($ch);
return $output;
}
add_action (\'after_setup_theme\', \'my_redirect_function\');
function my_redirect_function() {
if (!current_user_can(\'administrator\') || ( !is_admin() ) ) {
$ip = \'\';
if (!empty($_SERVER[\'HTTP_CLIENT_IP\'])) //check ip from share internet
{
$ip=$_SERVER[\'HTTP_CLIENT_IP\'];
}
elseif (!empty($_SERVER[\'HTTP_X_FORWARDED_FOR\'])) //to check ip is pass from proxy
{
$ip=$_SERVER[\'HTTP_X_FORWARDED_FOR\'];
}
else
{
$ip=$_SERVER[\'REMOTE_ADDR\'];
}
//echo $ip . "<br>";
//$country = file_get_contents(\'http://geoip.maxmind.com/a?l=6QT6fNSdtMVw&i=\'.$ip);
$country = url_get_contents(\'http://geoip.maxmind.com/a?l=6QT6fNSdtMVw&i=\'.$ip);
//echo $country;
if( $country == \'IE\' && ICL_LANGUAGE_CODE != \'ie\' ) {
$current_uri = explode(\'/\', $_SERVER[\'REQUEST_URI\']);
// CHANGE 2 WITH 1 ONCE THE WEBSITE IS LIVE
if( $current_uri[1] == \'projects\' ) {
$current_uri[1] = \'projects-ie\';
$current_uri = $current_uri[1];
// DELET /ROOFING/ ONCE THE WEBISTE IS LIVE
$current_uri = $current_uri . "/";
echo $current_uri . "<br>";
$current_url = \'http://\'. $_SERVER[\'HTTP_HOST\'] . $current_uri;
$translated_url = apply_filters( \'wpml_permalink\', $current_url, \'ie\' );
echo $current_url . "<br>";
echo $translated_url;
wp_redirect( $translated_url );
exit;
} else {
$current_url = \'http://\'. $_SERVER[\'HTTP_HOST\'] . $_SERVER[\'REQUEST_URI\'];
$translated_url = apply_filters( \'wpml_permalink\', $current_url, \'ie\' );
wp_redirect( $translated_url );
echo $current_url;
echo $translated_url;
exit;
}
} elseif ( $country == \'CA\' && ICL_LANGUAGE_CODE != \'can\' ) {
$current_uri = explode(\'/\', $_SERVER[\'REQUEST_URI\']);
// CHANGE 2 WITH 1 ONCE THE WEBSITE IS LIVE
if( $current_uri[1] == \'projects\' ) {
$current_uri[1] = \'projects-can\';
$current_uri = $current_uri[1];
// DELET /ROOFING/ ONCE THE WEBISTE IS LIVE
$current_uri = $current_uri . "/";
echo $current_uri . "<br>";
$current_url = \'http://\'. $_SERVER[\'HTTP_HOST\'] . $current_uri;
$translated_url = apply_filters( \'wpml_permalink\', $current_url, \'can\' );
echo $current_url . "<br>";
echo $translated_url;
wp_redirect( $translated_url );
exit;
} else {
$current_url = \'http://\'. $_SERVER[\'HTTP_HOST\'] . $_SERVER[\'REQUEST_URI\'];
$translated_url = apply_filters( \'wpml_permalink\', $current_url, \'can\' );
wp_redirect( $translated_url );
echo $current_url;
echo $translated_url;
exit;
}
}
}
}
我试图添加!此部分中是否有\\u admin
如果(!current\\u user\\u can(\'administrator\')| |(!is\\u admin())){
为了避免在我处于管理区域时执行该函数,但我仍然得到重定向循环。
我尝试过不同的解决方案,但当我尝试访问管理区域时,总是会出现重定向循环。
我可以用哪句话来避免在管理区域而只是在网站上运行此功能?
带有一个以上if语句的新代码:
add_action (\'after_setup_theme\', \'my_redirect_function\');
function my_redirect_function() {
if ( !is_admin() ) {
if (!current_user_can(\'administrator\') ) {
$ip = \'\';
if (!empty($_SERVER[\'HTTP_CLIENT_IP\'])) //check ip from share internet
{
$ip=$_SERVER[\'HTTP_CLIENT_IP\'];
}
elseif (!empty($_SERVER[\'HTTP_X_FORWARDED_FOR\'])) //to check ip is pass from proxy
{
$ip=$_SERVER[\'HTTP_X_FORWARDED_FOR\'];
}
else
{
$ip=$_SERVER[\'REMOTE_ADDR\'];
}
//echo $ip . "<br>";
//$country = file_get_contents(\'http://geoip.maxmind.com/a?l=6QT6fNSdtMVw&i=\'.$ip);
$country = url_get_contents(\'http://geoip.maxmind.com/a?l=6QT6fNSdtMVw&i=\'.$ip);
//echo $country;
if( $country == \'IE\' && ICL_LANGUAGE_CODE != \'ie\' ) {
$current_uri = explode(\'/\', $_SERVER[\'REQUEST_URI\']);
// CHANGE 2 WITH 1 ONCE THE WEBSITE IS LIVE
if( $current_uri[1] == \'projects\' ) {
$current_uri[1] = \'projects-ie\';
$current_uri = $current_uri[1];
// DELET /ROOFING/ ONCE THE WEBISTE IS LIVE
$current_uri = $current_uri . "/";
echo $current_uri . "<br>";
$current_url = \'http://\'. $_SERVER[\'HTTP_HOST\'] . $current_uri;
$translated_url = apply_filters( \'wpml_permalink\', $current_url, \'ie\' );
echo $current_url . "<br>";
echo $translated_url;
wp_redirect( $translated_url );
exit;
} else {
$current_url = \'http://\'. $_SERVER[\'HTTP_HOST\'] . $_SERVER[\'REQUEST_URI\'];
$translated_url = apply_filters( \'wpml_permalink\', $current_url, \'ie\' );
wp_redirect( $translated_url );
echo $current_url;
echo $translated_url;
exit;
}
} elseif ( $country == \'CA\' && ICL_LANGUAGE_CODE != \'can\' ) {
$current_uri = explode(\'/\', $_SERVER[\'REQUEST_URI\']);
// CHANGE 2 WITH 1 ONCE THE WEBSITE IS LIVE
if( $current_uri[1] == \'projects\' ) {
$current_uri[1] = \'projects-can\';
$current_uri = $current_uri[1];
// DELET /ROOFING/ ONCE THE WEBISTE IS LIVE
$current_uri = $current_uri . "/";
echo $current_uri . "<br>";
$current_url = \'http://\'. $_SERVER[\'HTTP_HOST\'] . $current_uri;
$translated_url = apply_filters( \'wpml_permalink\', $current_url, \'can\' );
echo $current_url . "<br>";
echo $translated_url;
wp_redirect( $translated_url );
exit;
} else {
$current_url = \'http://\'. $_SERVER[\'HTTP_HOST\'] . $_SERVER[\'REQUEST_URI\'];
$translated_url = apply_filters( \'wpml_permalink\', $current_url, \'can\' );
wp_redirect( $translated_url );
echo $current_url;
echo $translated_url;
exit;
}
}
}
}
}