If you build or maintain sites for clients, you may want your own administrator account kept out of the client’s Users screen so it doesn’t get edited or deleted by accident. The snippet below hides one admin from the list for everyone except that admin.
Hide a specific admin from the Users screen
Replace your-username with the login name of the account you want to hide:
add_action( 'pre_get_users', function ( $query ) {
if ( ! is_admin() || ! function_exists( 'get_current_screen' ) ) {
return;
}
$screen = get_current_screen();
if ( ! $screen || 'users' !== $screen->id ) {
return;
}
$hidden_login = 'your-username';
if ( wp_get_current_user()->user_login === $hidden_login ) {
return;
}
$hidden_user = get_user_by( 'login', $hidden_login );
if ( ! $hidden_user ) {
return;
}
$exclude = (array) $query->get( 'exclude' );
$exclude[] = $hidden_user->ID;
$query->set( 'exclude', $exclude );
} );Code language: PHP (php)
Here is what it does:
- It only runs on the Users screen in the admin, so author lists, author boxes and other plugins that query users elsewhere still see the account.
- The hidden admin still sees themselves in the list, so they can edit their own profile.
- It adds the user to the query’s
excludelist instead of editing the raw SQL. The 2014 version of this snippet rewrotequery_wherewithstr_replace(), which breaks when WordPress changes how it builds the query.
Hide the user counts
The links above the list (“All (5) | Administrator (2)”) still count the hidden account. The simplest fix is to hide the counts on that screen:
add_action( 'admin_head-users.php', function () {
echo '<style>.subsubsub .count { display: none; }</style>';
} );Code language: PHP (php)
This is not a security measure
Hiding an account from one screen only keeps it out of sight. Another administrator can still open it directly at user-edit.php?user_id=. If the account has published posts, its author archive and the public /wp-json/wp/v2/users endpoint still show it. If the real goal is to stop a client from managing your account, give them the Editor role, or a custom role without the edit_users and delete_users capabilities, instead of a second administrator account.
Two related tweaks for client sites: hiding a plugin from the Plugins list and removing the WordPress version from the admin footer.
Where to add this code
Add the snippet with a code snippets plugin instead of your theme’s functions.php, so it survives theme updates and you can switch it off without FTP. Here is how to add custom code snippets safely in WordPress with the free Code Snippets plugin. On my own sites I use WPCodeBox, which keeps snippets in a cloud library so the same snippet can go on several sites. Not sure which plugin to choose? I compared Code Snippets, WPCode and WPCodeBox by feeding each one broken snippets. (Marius)
Disclosure: the WPCodeBox link is an affiliate link. If you buy through it, I earn a commission at no extra cost to you.
Thanks for this. Just what I needed.
Thanks,
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?
Working fine with me. Make sure to remove < and > from .
Duh,
Once again, Thanks you so much.
Nice – 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.
Please send me the snippet copy :)
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');
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
Hey,
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!
Thanks a lot for the suggestion :) I have updated the article with this snippet :)
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);
}
}
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
Thanks bro…
but this does work on WP 4+?
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