Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

(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.

share|improve this question

5 Answers 5

up vote 8 down vote accepted

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);
  }
}
share|improve this answer
1  
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 '11 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 '11 at 23:22
    
@EAMann - True, I was being convenient. If someone needs more I'll look into it. –  MikeSchinkel Mar 1 '11 at 3:33
    
@Carlos - Do you need v3.0.x, or is v3.1 okay? –  MikeSchinkel Mar 1 '11 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 '11 at 7:20

Howdy_McGee's answer works well. I used in conjunction with members plugin to elevate editors to add users and edit menus but not mess with other admin tasks, giving them a lot of control over the site but making it very hard for them to break it.

share|improve this answer
    
welcome to the site. This is a borderline answer, can you please expand on what plugins did you use and how to make it useful for other people as well? –  Mark Kaplun Feb 14 at 18:39

User Levels are depreciated so this method checks against capabilities instead.

/** Hide Administrator From User List **/
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');
share|improve this answer

pre_user_query action has been deprecated from wordpress.org. Replace it with wp_user_query and it will work fine with WordPress 3.5.x.

share|improve this answer
    
wp_user_query does not work in 3.6.1, however pre_user_query does. dont know about 3.5.x –  gwillie Sep 27 '13 at 8:37
    
Are you certain? Can you cite a source? This action is used in the current trunk - see wp-includes/user.php, line 549‌​. –  Johannes Pille Sep 27 '13 at 9:48

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

    }
}
share|improve this answer
    
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 '13 at 14:26

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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