How to disable login by roles?

Hello! Guys tell me what's wrong, I need to restrict login to your account for easy customers (role), here such code:

//here try different variants, but does not work
add_filter('authenticate',function($user,$username) {
 if (!is_wp_error($user)) {
$auth_user=get_user_by('login',$username);
 if ($auth_user && !is_user_role_in( array('administrator','dropshipper') ) ) {
 return new WP_Error('authentication_failed', '<p class="message"><b>Personal account disabled. Purchases are made without registration.</b></p>');
}
}
 return $user;
},100,2);




## Checks whether the specified role the role of the current/a specified user
## $roles string/array - the name of the role you want to validate the current user
/*
// Example usage
if( is_user_role_in( array('new_role','new_role2') ))

 echo 'current user Role new_role or new_role2';
*/
function is_user_role_in( $roles, $user = false ){
 if( ! $user ) $user = wp_get_current_user();
 if( is_numeric($user) ) $user = get_userdata( $user );

 if( empty($user->ID) )
 return false;

 foreach( (array) $roles as $role )
 if( isset($user->caps[ $role ]) || in_array($role, $user->roles) )
 return true;

 return false;
}
April 19th 20 at 12:15
2 answers
April 19th 20 at 12:17
Solution
add_filter('authenticate','filter_function_name_4601');
filter_function_name_4601 function($user){
if(isset($_POST['log'])){
$username=$_POST['log'];
 if(isset($username)){$user=get_user_by('login',$username);$user_data=get_object_vars($user);} 
if(isset($user_data)){$userId=$user_data["ID"];$u_meta=get_userdata($userId);$u_roles=$u_meta->roles;}
 } 
 if(!empty($u_roles)&&(in_array('administrator',$u_roles,true)||in_array('editor',$u_roles,true))){//iterate through the roles. who want to ban
$Who=in_array('administrator',$u_roles,true)?__('Administrators','VAB'):__('Editors','VAB');
 wp_die($Who.' '.__('authorization allowed','VAB'));//write the message for them
}else{
 return $user;
}
}

The result, depending on who was breaking
5e9849612ddb8834465415.jpeg
5e9849ed43f95972809011.jpeg

using username
add_filter('authenticate','filter_function_name_4601',10,3);
filter_function_name_4601 function($user,$username,$password){
if($username){
 if(isset($username)){$user=get_user_by('login',$username);$user_data=get_object_vars($user);} 
if(isset($user_data)){$userId=$user_data["ID"];$u_meta=get_userdata($userId);$u_roles=$u_meta->roles;}
 } 
 if(!empty($u_roles)&&(in_array('administrator',$u_roles,true)||in_array('editor',$u_roles,true))){//iterate through the roles. who want to ban
$Who=in_array('administrator',$u_roles,true)?__('Administrators','VAB'):__('Editors','VAB');
 wp_die($Who.' '.__('authorization allowed','VAB'));//write the message for them
}else{
 return $user;
}
}

this, if developed tomorrow log will change to something else...
thank you Paul Internet Abril...
Customers hundreds crammed and put the website on wdsc...
Optimizations bukomero accidentally do? A day visit a couple of thousand registered customers, and continuously from memory put... made a purchase without registering and forbade the entrance to the admin, but it looks like the load is not greatly decreased - queen_Prosacco commented on April 19th 20 at 12:20
@queen_Prosacco, but not for that..
Optimizations bukomero accidentally do?

faced with bukomero... on LAN is only looked briefly, what kind of plugin... - Anabel.Barrows commented on April 19th 20 at 12:23
April 19th 20 at 12:19
Option one: using the plugin Remove Dashboard Access. You can download it here: https://wordpress.org/plugins/remove-dashboard-acc...

Option two: Add the following code to the file functions.php Your active theme. Administrator is insert your desired role. The user attempted to log in to your Wordpress admin panel you will get a message about 404 error.

true_wp_admin_block function() {
 if (!current_user_can('administrator')) { // if not administrator
 header('HTTP/1.0 404 Not Found');
exit();
 } 
}
add_action('admin_menu', 'true_wp_admin_block');


In WordPress by default already there are 6 groups of users:

  1. Super Admin — a super administrator, who has the right to operate a network of sites.
  2. Administrator — the administrator.
  3. Editor — an editor can publish and edit posts of other users.
  4. Author — an author can publish and edit own posts.
  5. Contributor — the member may write and submit posts for moderation.
  6. Subscriber — the subscriber, all he can do is edit your profile.
they're not in the admin area... and crammed on the page my-account - store....

add_action('admin_menu', 'true_wp_admin_block')
----
I'll see if he and boutiques't work

I have a couple of custom roles, and should be banned for custom role of "customer"

thank you - queen_Prosacco commented on April 19th 20 at 12:22
@queen_Prosacco, authorization will take place anyway yeah, but in admin area you come... - Anabel.Barrows commented on April 19th 20 at 12:25
in the admin area they are Yin, they should not be woocomerce account - queen_Prosacco commented on April 19th 20 at 12:28

Find more questions by tags WooCommerceWordPress