3

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?

2
  • How about adding an OR-statement to your Query? Probably like this (untested): NOT LIKE '$administrator%' OR '%editor%'
    – flomei
    May 28, 2015 at 14:51
  • No, that doesn't work... May 28, 2015 at 19:11

1 Answer 1

7

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');
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.