We can block the user login by the Role. This will work before the user name and password validation. So we can block the user before the validation for this we need to assign the user to particular role in this I used the "blocked" role which is custom one you can make any role to get blocked.
Sample code to block the user by role.Just paste the
following code in either plugin files or in the functions.php ( theme
file )
add_filter('wp_authenticate_user', 'custom_block_user_role_login', 10, 2);
if(!function_exists('custom_block_user_role_login')) {
function custom_block_user_role_login($user, $password) {
$role = $user->wp_capabilities;
/*
* In below code "blocked" is the user role you can assign it to another role
* which you want to get blocked
*/
if(array_key_exists('blocked', $role)) {
return new WP_Error('block_user', __('You are Blocked.'));
}
return $user;
}
}
Oshine is a popular, creative WordPress theme used by 28000+ customers across the world. From Portfolio, Photography, Agency, Blog, Architecture, Business or Shop, build beautiful websites for any purpose with ease.
ReplyDelete