Skip to content

How To Hide A Specific Admin From User List In WordPress

I was working on a plugin for one of my clients, which required me to hide the admin from the user list in WordPress for some reasons.

I made some Google searches, but found nothing what I really needed. So, I took this code from Stack Overflow and modified it.

Here are a couple of snippets which will hide a specific admin from user list in WordPress for everyone or for everyone but the user itself.

So, this modified snippet will hide your admin user from the list for everyone:

add_action('pre_user_query','yoursite_pre_user_query');
function yoursite_pre_user_query($user_search) {
  global $current_user;
  $username = $current_user->user_login;

    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
      "WHERE 1=1 AND {$wpdb->users}.user_login != '<YOUR USERNAME>'",$user_search->query_where);

}

Replace <YOUR USERNAME> in above code with your username. But now who in this world your admin user would itself change its login details?

So, I created this code which will hide your admin user from appearing in the list for everyone, but the user itself:

add_action('pre_user_query','yoursite_pre_user_query');
function yoursite_pre_user_query($user_search) {
  global $current_user;
  $username = $current_user->user_login;

  if ($username == '<YOUR USERNAME>') { 

  }

  else {
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
      "WHERE 1=1 AND {$wpdb->users}.user_login != '<YOUR USERNAME>'",$user_search->query_where);
  }
}

Replace <YOUR USERNAME> in above code with your username. Problem solved, right? NO!

There is one more small problem. WordPress will still show number of admin users in the top navigation of user list. So, we will just remove the count.

Just add following snippet to your current theme’s functions.php file:

function hide_user_count(){
?>
<style>
.wp-admin.users-php span.count {display: none;}
</style>
<?php
}

add_action('admin_head','hide_user_count');

 

14 thoughts on “How To Hide A Specific Admin From User List In WordPress”

  1. Thanks Hardeep,
    I have placed both snippets in the functions.php of the parent theme and edited the Your Username, but the user still shows up in the list. I’m running the latest version of wordpress, would that be a factor?

  2. Nice hardeep – Still think I am missing something as I added both and the numbers still show – but the biggest one is all the users are gone, not just the one I wanted to remove. any suggestions? thanks.

      1. Hardeep – thanks for your help – I will send money your way for this.

        I got the numbers to work – so those are gone. but the other other part is still messed up – here is what i put in functions and all the users disappear.

        add_action(‘pre_user_query’,’yoursite_pre_user_query’);
        function yoursite_pre_user_query($user_search) {
        global $current_user;
        $username = $current_user->user_login;

        if ($username == ”) {

        }

        else {
        global $wpdb;
        $user_search->query_where = str_replace(‘WHERE 1=1’,
        “WHERE 1=1 {$wpdb->users}.user_login != ””,$user_search->query_where);
        }
        }

        function hide_user_count(){
        ?>

        span.count {display:none;}

        <?php
        }

        add_action('admin_head','hide_user_count');

        1. Hey,

          Please do me a favor. If you want me to look into the issue then please use this site’s contact form to send me login & FTP credentials, so I could check it. Also, don’t forget to tell me which admin user you want to hide :)

          Thanks

  3. Hey Hardeep,

    I would suggest small modification to your code for hiding count. The way you presented it, all the counts are getting hidden, for plugins, posts, pages, everything – while we only want to hide users count.
    So something like that would work better for the needs I think:

    .wp-admin.users-php span.count {display: none;}

    Cheers for all the rest though of your snippets! Works like a charm!

    Regards!

  4. Just improving the code, you’ll try this:

    add_action(‘pre_user_query’,’yoursite_pre_user_query’);
    function yoursite_pre_user_query($user_search) {
    global $current_user;
    $username = $current_user->user_login;
    if ($username != ”) {
    global $wpdb;
    $user_search->query_where = str_replace(‘WHERE 1=1’, “WHERE 1=1 AND {$wpdb->users}.user_login != ””,$user_search->query_where);
    }
    }

  5. Thank for the code, it did worked very well. However, do you have anyway to reduce the user count instead of hiding it. Hiding it isn’t make sense.

    Thank

  6. Thank you for tutorial , when i try to change codes in function.php and after save my site crash and i see blank page and i should restore original file then site works , i do everything right but have problem

Leave a Reply to thesherps Cancel reply

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