如果我在管理区域,请避免执行函数(重定向

时间:2018-05-02 作者:Crashy

我已经建立了这个功能,允许我根据用户的国家/地区重定向用户,它似乎适用于除管理部分以外的所有页面。显然,如果我在管理部分,我不想重定向,但目前我得到了一个重定向循环和一个错误。

这是我的职责:

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;
           }
         }

    }
}
}

1 个回复
SO网友:Celso Bessa

没有WordPress核心函数来检查我们是否在登录或注册页面,但我们可以使用$pagenow 如上所述,全球in this answer.

function is_login_page() {
    return in_array( $GLOBALS[\'pagenow\'], array( \'wp-login.php\', \'wp-register.php\' ) );
}
然后我们可以在Guard Clause, 这是一个很好的方式avoid nested conditionals:

function my_redirect_function() {

    // exit early if is in admin site or login/register pages
    if ( is_admin() || is_login_page() ) {
        return;
    }

    // check for a specific capability is a better practice as we can have
    // a more granular control of who can or can\'t do something
    if ( !current_user_can( \'manage_options\' ) ) {
        // do your thing in here
    }
}
我希望这有帮助。请注意,我检查了specific capability 而不是角色,因为它允许对谁能做什么或不能做什么进行更精细的控制。

结束

相关推荐

自定义插件提供:wp-admin/admin-ajax.php 400(错误请求)

我在调试我正在编写的插件时遇到问题。我想在用户更改select输入的选项时执行Ajax请求。事情是,它昨天完全按照预期工作,然后当我今天测试网站时(从昨天起,没有进行任何代码更改),我得到一个:领域com/wp-admin/admin-ajax。php 400(错误请求)我的Chrome控制台中的消息。如果我在Internet Explorer/FireFox中尝试,它不会在控制台中给出错误,但Ajax请求仍然不起作用。我正在创建的插件是一个DigitalOcean droplet,它是使用WordPre