Hide a specific admin user from WordPress user list

Hide a specific admin user from WordPress user list, If you want to hide a specific user from WordPress user list in administrative panel, then you can do this by adding this below code to theme functions file. And after that adding an action to execute that function. Using this code you can hide any user role like editor, contributor or even if it’s one of the administrator.

Code to Hide a specific admin user from WordPress user list

Add this code in functions.php file in your current theme, this code will hides specified wordpress user from wordpress user list. Replace with WordPress username which you want to hide.

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

Above code will hides that specific user for every logged in user, by using this below you can the hidden user and you can hide it for every other except you. Replace with the username you want to hide.

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

After adding above code, specific user name will be hidden and the count will be shown all the users count. Add this code in functions.php file to hide users count. This is only admin function, be careful while adding this function because you need to close php block and reopen it.

function hide_user_count(){
?>
<style>
span.count {display:none;}
</style>
<?php
}
 
add_action('admin_head','hide_user_count');

Sources :

Hide A Particular Admin Account From WordPress User List
How To Hide A Specific Admin From User List In WordPress

Vivek Vengala
 

Vivek Vengala is a Online Entrepreneur, Web Developer from Hyderabad India.

Click Here to Leave a Comment Below 0 comments

Leave a Reply:

x

Join My Facebook Group to Receive Coding Tutorial for Free

email-subsribe