Hide a WP user account

October 20, 2021
Code Category:

Basically hides a user account unless you're the user account. Used on a project where I had to allow other Admin users, but I didn't want them messing with my account.

/*
    Hide the Fotan user account on the Dashboard Users screen from everyone but me. 
*/
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 != 'fotan') { 
    global $wpdb;
    $user_search->query_where = str_replace('WHERE 1=1',
      "WHERE 1=1 AND {$wpdb->users}.user_login != 'fotan'",$user_search->query_where);
  }
}
©2021 Fotan Web Design