I have a snippet in my functions.php that removes the admin role from the “change role to” drop down menu in the users listing screen, so that editors who can list users and manage roles won’t be able to turn another user into an admin. The code below works perfectly.

function isa_pre_user_query($user_search) {
$user = wp_get_current_user();
  if (!current_user_can('administrator')) { // Is Not Administrator - Remove Administrator
    global $wpdb;

$user_search->query_where =
    str_replace('WHERE 1=1',
        "WHERE 1=1 AND {$wpdb->users}.ID IN (
             SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
                WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->prefix}capabilities'
                AND {$wpdb->usermeta}.meta_value NOT LIKE '%administrator%')",
        $user_search->query_where
    );
  }
}
add_action('pre_user_query','isa_pre_user_query');

Now, my question is, how can I also have the Editor role removed from that drop down, so that Editors cannot create other editors? I want to remove both the Admin and the Editor role from that drop-down menu, and restrict the role choices only to Author, Contributor and Subscriber.

Any ideas?

1 Answer
1

Try using below code to remove administrator and editor option from drop down. Use
editable_roles filter

function wdm_user_role_dropdown($all_roles) {

    global $pagenow;

    if( current_user_can('editor') && $pagenow == 'user-edit.php' ) {
        // if current user is editor AND current page is edit user page
        unset($all_roles['administrator']);
        unset($all_roles['editor']);
    }

    return $all_roles;
}
add_filter('editable_roles','wdm_user_role_dropdown');

Leave a Reply

Your email address will not be published. Required fields are marked *