在点击wp-admin时,如何将非管理员和最终用户重定向到家

时间:2017-05-07 22:03:25

标签: wordpress .htaccess wp-admin

当外部用户或非管理员尝试访问http://www.urlVisibleToUsers.com/wp-admin时,会重新定向到错误页面,但仍然会显示home_url(WP安装所在的位置)。我希望能够将所有最终用户或任何不是管理员的角色重定向到http://www.urlVisibleToUsers.com/并阻止调用中断。我在functions.php中有以下代码,但外部用户仍会在导航栏中看到home_url地址(尽管会显示错误页面):

add_action( 'admin_init', 'admin_area_for_manage_options_only');
function admin_area_for_manage_options_only() {

      if( defined('DOING_AJAX') && DOING_AJAX ) {
            //Allow ajax calls in order to have ALM working
            return;
      }

      if( ! current_user_can( "manage_options" ) ) {
           //Redirect to main page if the user has no "manage_options" capability
           wp_redirect( get_site_url( ) );
           exit();
      }
 }

不确定为什么上面的代码不起作用,这是正确的方法吗?我应该在我的.htaccess中引入Apache重定向规则吗?

2 个答案:

答案 0 :(得分:1)

将代码用作插件,主题函数对于某种操作/过滤器挂钩运行得非常晚。

更好的是,只需将其添加为mu-plugin,无需安装,无法通过管理面板禁用:https://codex.wordpress.org/Must_Use_Plugins

<?php
/**
 * Plugin Name: Admin only for admins
 */

add_action( 'admin_init', function(){
      if( defined('DOING_AJAX') && DOING_AJAX ) {
            return;
      }    
      if( ! current_user_can( "manage_options" ) ) {
           wp_redirect( get_site_url( ) );
           exit();
      }
 });

答案 1 :(得分:1)

你的functions.php中的

把这段代码

 private void handleSigninResult(GoogleSignInResult sig) {
    if(sig.isSuccess()){

        GoogleSignInAccount acc =  sig.getSignInAccount();
        //accessing  the data
        name.setText(acc.getDisplayName());
       SharedPreferences prefs = getSharedPreferences("shared_pref_name", MODE_PRIVATE);
       SharedPreferences.Editor editor = prefs.edit();
       editor.putString("email", acc.getEmail());
       editor.putInt("name", acc.getDisplayName());
       editor.putBoolean("hasLogin",true);
       editor.apply();

        //image with glide
        Glide.with(this).load(acc.getPhotoUrl()).into(photo);

    }else {
        //in case is not successful
        //send the user to the Login Screen
        goLoginInScree();
    }    
}
相关问题