dongzi3434 2016-05-17 07:04
浏览 43
已采纳

只有管​​理员允许访问wp-admin和登录?

I would like to do that only administrator(role) allow to login with wp-admin not any other role, other uses like(editor,author) are login from front end and it's working fine but i would like only administrator can login through wp-admin.

I have used ultimate member plug in for front end login. Ultimate Plugin link

also i have used below code for access wp-admin only for administrator(role) but it's not working.

<?php
function restrict_admin(){
//if not administrator, kill WordPress execution and provide a message
   if( !current_user_can('administrator') ) {
        wp_die( __('You are not allowed to access this part of the site') );
    }
}
add_action( 'admin_init', 'restrict_admin', 1 );
?>
  • 写回答

4条回答 默认 最新

  • duanhe1903 2016-05-17 11:17
    关注

    Thank you guys for your help, I am answering my own question hope this will help others too.

    I have referred this link https://developer.wordpress.org/reference/functions/wp_authenticate/

    I have solved this with hook wp_authenticate

     add_action( 'wp_authenticate' , 'check_custom_authentication' );
      function check_custom_authentication ( $username ) {
    
        $username;
         $user = new WP_User($username);
         $user_role_member=$user->roles[0];
    
    
    
        if($user_role_member == 'author' || $user_role_member == 'editor'){
            session_destroy();
            wp_redirect( home_url() );
            exit;
        }
    
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 用hfss做微带贴片阵列天线的时候分析设置有问题
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答