[英]Only administrator allow to access wp-admin and login?
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. 我想做的是只有Administrator(role)允许使用wp-admin登录,没有任何其他角色,其他用途,例如(editor,author)是从前端登录,并且工作正常,但我希望只有Administrator可以通过wp登录-管理员。
I have used ultimate member plug in for front end login. 我已使用Ultimate Member Plugin进行前端登录。 Ultimate Plugin link 终极插件链接
also i have used below code for access wp-admin only for administrator(role) but it's not working. 我也使用下面的代码访问wp-admin仅用于Administrator(role),但它不起作用。
<?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 );
?>
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/ 我已经引用了此链接https://developer.wordpress.org/reference/functions/wp_authenticate/
I have solved this with hook 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;
}
}
add_action( 'admin_init', 'redirect_none_admin' );
function redirect_none_admin(){
if(is_admin() && current_user_can(activate_plugins)){
//...
}else{
wp_redirect(home_url());
}
}
i tested this one and it worked i think it is simple and easy you can find Roles and Capabilities here 我测试了这一功能,它确实有效,我认为这很简单,您可以在这里找到“ 角色和功能”
Try this 尝试这个
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;
}
}
If anyone will need the same here is code that allows only administrators, authors and editors to login using /wp-login.php 如果有人需要相同的代码,则此代码仅允许管理员,作者和编辑者使用/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 ------------------
You can add\\remove roles in the array above array('administrator','author','editor') 您可以在array上方的数组中添加/删除角色(“ administrator”,“ author”,“ editor”)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.