2016-05-17 88 views
1

我想这样做只有管理员(角色)允许登录wp-admin没有任何其他角色,其他用途像(编辑器,作者)是从前端登录,它工作正常,但我只想管理员可以登录通过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); 
?> 
+0

你可以使用[此插件(https://wordpress.org/plugins/解决了这个除去仪表板存取换非管理员/)。安装后,转至设置>仪表板访问权限,然后选择“仅管理员”。 – vard

+0

这件事情正在工作,但我只需要管理员(角色)可以通过wp-admin登录而不是编辑器,作者和订户用户可以通过wp-admin登录。 –

+0

如果您选择正确的选项(仅限管理员),就像我告诉您的那样,只有管理员可以访问wp-admin ... – vard

回答

3

谢谢你们的帮助,我正在回答我自己的问题,希望这也能帮助其他人。

我已经提到这个链接https://developer.wordpress.org/reference/functions/wp_authenticate/

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

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

我也尝试过这种方式,但它不工作。编辑,作者用户仍然登录。 –

+0

@sameersheikh我编辑过它,现在就试试。 –

+0

@hermnath仍然无法使用,仍然是作者和编辑的用户登录。 –

0
add_action('admin_init', 'redirect_none_admin'); 

function redirect_none_admin(){ 

if(is_admin() && current_user_can(activate_plugins)){ 
    //... 
}else{ 
    wp_redirect(home_url()); 
} 
} 

我测试的这一个,它的工作,我认为这是简单和容易 你可以找到Roles and Capabilities这里

相关问题