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

时间:2016-05-17 07:04:01

标签: php wordpress

我想这样做只有管理员(角色)允许使用wp-admin登录而不是任何其他角色,其他用途如(编辑,作者)从前端登录并且它工作正常但我会只有管​​理员可以通过wp-admin登录。

我使用终极会员插件进行前端登录。 Ultimate Plugin link

我也使用以下代码仅为管理员(角色)访问wp-admin但它无效。

<?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 个答案:

答案 0 :(得分:3)

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

 }

答案 1 :(得分:1)

$ str="1,10,100,1\,000,10\,000,100\,000"
$ sed 's/\([^\]\),/\1\n/g' <<< $str
1
10
100
1\,000
10\,000
100\,000

我测试了这个,它起作用我认为它简单易行 你可以在这里找到Roles and Capabilities

答案 2 :(得分:0)

试试这个

add_action( 'admin_init', 'redirect_non_admin_users' );
/**
 * Redirect non-admin users to home page
 *
 * This function is attached to the 'admin_init' action hook.
 */
function redirect_non_admin_users() {
    if ( is_admin() !== false) {
        wp_redirect( home_url() );
        exit;
    }
}

答案 3 :(得分:0)

如果有人需要同样的代码,这里的代码仅允许管理员,作者和编辑者使用/wp-login.php

登录
//------------- This website is read only - so only staff can log in  -------------
add_filter( 'authenticate', 'myplugin_auth_signon', 30, 3 );
function myplugin_auth_signon( $user, $username, $password ) {
    $user_role_member=$user->roles[0];
    if(!in_array($user_role_member,array('administrator','author','editor'))){
         wp_logout();
         return new WP_Error( 'broke', __( "This website is read only for regular users", "your_wp_domain_name" ) );
        exit;
    }else{
        return $user;   
    }
}
//------------- this website is read only - so staff can log in  ------------------

您可以在阵列上方的阵列中添加/删除角色(“管理员”,“作者”,“编辑者”)

相关问题