Change WordPress user roles and capabilities › Forums › Restrict or Permit access inside WordPress – how to › Limit role promotion, but maintain visibility of other roles. › Reply To: Limit role promotion, but maintain visibility of other roles.
07/01/2023 at 05:41
#8134
Vladimir
Keymaster
You can exclude role ‘board_member’ from all user role selecting drop-down lists exactly for the user with role ‘coordinator’ using the code below:
add_filter('editable_roles', 'ure_exclude_role', 10, 1);
function ure_exclude_role( $roles ) {
$user = wp_get_current_user();
if ( empty( $user ) || empty( $user->roles ) ) {
return $roles;
}
if ( !in_array( 'coordinator', $user->roles ) ) {
return $roles;
}
// Remove board_member role from all dropdown user roles lists
if ( isset( $roles['board_member'] ) ) {
unset( $roles['board_member'] );
}
return $roles;
}
Also you can fully exclude roles and users with those roles from the point of view user with coordinator role with the help of “Other roles access” add-on: