You can make an array of the roles that need to be blocked, and use array_intersect()
to check if the current user is in any of those roles.
function wpse66094_no_admin_access() {
$redirect = isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : home_url( "https://wordpress.stackexchange.com/" );
global $current_user;
$blocked_roles = array(
'shopkeeper',
'block-this-role',
'block-this-role-too'
);
$user_roles = $current_user->roles;
// If the user is in any of the blocked roles, redirect 'em.
if ( ! empty( array_intersect( $user_roles, $blocked_roles ) ) ) {
wp_safe_redirect( $redirect );
exit;
}
}
add_action( 'admin_init', 'wpse66094_no_admin_access', 100 );
Thanks to @Cornell Raiu in the comments for correcting the wp_redirect()
/ exit
code, and @TomJNowell for recommending wp_safe_redirect()
.