15

(Moderator's note: Original title was "Remove Admin from User Menu")

I have created a client administrator role which is essentially an Editor with ability to add/remove users. The article "Editor can create any new user except administrator" was excellent in helping keep my new client admin role from editing or creating a True admin user.

However what would be ideal is to hide administrators from client admins when they are viewing users. I want them to "believe" that they are the admin of their site but I do not want them to be able to even view my role/user--essentially hiding the "administrator" role from them when they are in the "Users" panel.

4 Answers 4

11

Hi @Carlos:

Try adding the following to your theme's functions.php file, or in a .php file within a plugin that you might be writing (which works for WordPress 3.1.x):

add_action('pre_user_query','yoursite_pre_user_query');
function yoursite_pre_user_query($user_search) {
  $user = wp_get_current_user();
  if ($user->ID!=1) { // Is not administrator, remove administrator
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
      "WHERE 1=1 AND {$wpdb->users}.ID<>1",$user_search->query_where);
  }
}

If you have WordPress 3.0.x try this instead (since WordPress didn't add the 'pre_user_query' hook until 3.1):

add_action('pre_user_search','yoursite_pre_user_search');
function yoursite_pre_user_search($user_search) {
  $user = wp_get_current_user();
  if ($user->ID!=1) { // Is not administrator, remove administrator
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
      "WHERE 1=1 AND {$wpdb->users}.ID<>1",$user_search->query_where);
  }
}
11
  • 2
    That only hides the first user added (which is almost always the admin) ... but if the administrator has an ID other than 1 you'll have to change the query appropriately. It will also only hide 1 administrator, not all administrators.
    – EAMann
    Feb 28, 2011 at 19:50
  • I could actually only get this to work on version 3.1. Same code on any older versions doesn't seem to do the trick (3.04 even).
    – Carlos
    Feb 28, 2011 at 23:22
  • @EAMann - True, I was being convenient. If someone needs more I'll look into it. Mar 1, 2011 at 3:33
  • @Carlos - Do you need v3.0.x, or is v3.1 okay? Mar 1, 2011 at 3:33
  • @Mike—wow, yes if you would be willing to help me out with that it would be wonderful. I have been hacking away to no avail. For my purposes Admin ID 1 works just fine. I really appreciate it.
    – Carlos
    Mar 1, 2011 at 7:20
10

Here's a mod to MikeSchinkel's answer that checks if the current user has a role of administrator and if not it only selects users that are subscribers.

add_action('pre_user_query','yoursite_pre_user_query');
function yoursite_pre_user_query($user_search) {
    $user = wp_get_current_user();

    if ( $user->roles[0] != '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}user_level' 
                    AND {$wpdb->usermeta}.meta_value = 0)", 
            $user_search->query_where
        );

    }
}
1
  • 1
    Just FYI For anyone in the future that's not very proficient at SQL such as myself, if you want to show all users that aren't administrators (Editors, Authors, Subscribers etc.) but still hide administrators, you could change this line: AND {$wpdb->usermeta}.meta_value = 0 and instead make the meta value < 10 like so: AND {$wpdb->usermeta}.meta_value < 10) which will show all users and hide all administrators from all users no matter what their level is.
    – Howdy_McGee
    Aug 19, 2013 at 14:26
2

User Levels are deprecated, so this method checks against capabilities instead:

/** Hide Administrator From User List **/
function isa_pre_user_query( $user_search ) {
    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' );
1

pre_user_query action can be used to alter the user query since WordPress 3.1.0

2
  • 1
    wp_user_query does not work in 3.6.1, however pre_user_query does. dont know about 3.5.x
    – gwillie
    Sep 27, 2013 at 8:37
  • 1
    Are you certain? Can you cite a source? This action is used in the current trunk - see wp-includes/user.php, line 549. Sep 27, 2013 at 9:48

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