最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Remove admin AND editor from the "change role to" menu in user listing

programmeradmin3浏览0评论

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?

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?

Share Improve this question asked May 28, 2015 at 14:09 Ismael LatorreIsmael Latorre 451 silver badge7 bronze badges 2
  • How about adding an OR-statement to your Query? Probably like this (untested): NOT LIKE '$administrator%' OR '%editor%' – flomei Commented May 28, 2015 at 14:51
  • No, that doesn't work... – Ismael Latorre Commented May 28, 2015 at 19:11
Add a comment  | 

1 Answer 1

Reset to default 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)

  1. 暂无评论