dslpofp041310584 2017-03-15 19:57
浏览 49
已采纳

除非在WordPress中用户名是X(不是用户角色),如何阻止管理区域?

I have a site where I want some users to have the role type of admin but still not be able to access the admin area (don't ask!) This is more of a temporary fix whilst my new site is being built.

I am using this code at the moment which blocks everyone to the admin area unless role type is admin - but how can I block admin unless username is 'mack' for example.

add_action( 'init', 'blockusers_init' );
function blockusers_init() {
    if ( is_admin() && ! current_user_can( 'administrator' ) &&
        ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) ) {
      wp_redirect( home_url() );
      exit;
    }
}

Obviously the user still needs to access all other front end pages of the site, just not the admin area.

  • 写回答

2条回答 默认 最新

  • doumiebiao6827 2017-03-15 20:09
    关注

    If you want a temporary fix without working in your code maybe can you disable user

    https://srd.wordpress.org/plugins/disable-users/

    Otherwise as @Eduardo say you can do something like this

    add_action( 'init', 'blockusers_init' );
    function blockusers_init() {
        $current_user = wp_get_current_user();
        if ( is_admin() && ! current_user_can( 'administrator' ) &&
            ! ( defined( 'DOING_AJAX' ) && DOING_AJAX ) && $current_user->user_login != 'mack' ) {
          wp_redirect( home_url() );
          exit;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案