2

I'm using the below code to hide a user from the WordPress User List:

add_action( 'pre_user_query', 'ap_pre_user_query' );
function ap_pre_user_query( $user_search ) {
    $user = wp_get_current_user();
    if ( $user->ID != 1 ) { // Is not administrator, remove administrator (you can add any user-ID)
        global $wpdb;
        $user_search->query_where = str_replace( 
            'WHERE 1=1',
            "WHERE 1=1 AND {$wpdb->users} . ID<>1", 
            $user_search->query_where 
        );
    }
}

The code is working fine, but it's counted in the user list; it shows (2), while I want to show (1) only for my client's user. Moreover, it can be accessed from the frontend by going to the author URL:

http://domain.com/author/USER

Any suggestions?

1
  • These are two different question. Author archive doesn't have to do anything with user query. – Sumit Jun 23 '16 at 7:25
5

You're wanting to hide a user from WordPress in three places:

  1. The user list in the admin
  2. The count above that user list
  3. The user's author archive on the front-end

As you mentioned, you've already solved item 1 and you've included the code for that. So, I'll provide the solutions for items 2 and 3.

The user you want to hide is an administrator with a user ID of 1, so I'm working on that assumption. I've also assumed it's the only administrator user there. For anyone who comes across this answer later, you'll need to modify the code accordingly if you want to hide a different user/user group.

Modifying the user counts

Unfortunately the count_users() function isn't filterable. But, the user counts are shown within the hyperlinks to the different user views, which are output by the WP_List_Table::views() function. These views are filterable using a dynamic filter, views_{$this->screen->id}, which on the users page, translates to views_users.

So, we're going to hook into this filter and do two things: remove the Administrator view completely, and decrease the count by 1 in the All view. We'll only do this if the currently logged in user isn't our superuser (ID === 1).

add_filter( 'views_users', 'wpse230495_modify_user_views' );

function wpse230495_modify_user_views( $views ) {

  if( get_current_user_id() === 1 ) { return $views; } // bow out if we're user number 1

  // filter the 'all' count and remove 1 from it
  $views['all'] = preg_replace_callback(
    '/\(([0-9]+)\)/',
     function( $matches ){ return '(' . ( $matches[1] - 1 ) . ')'; },
     $views['all']
  );

  // filter the 'administrator' count and remove 1 from it
  $views['administrator'] = preg_replace_callback(
    '/\(([0-9]+)\)/',
     function( $matches ){ return '(' . ( $matches[1] - 1 ) . ')'; },
     $views['administrator']
  );

  // alternatively, uncomment next line if you want to remove Administrator view completely
  // unset( $views["administrator"] );

  return $views;

}

Hiding a user's author archive on the front-end

This is a bit simpler. All we're going to do here is hook into the template_redirect action to redirect a visitor back to the home page if they try to access the author archive of our privileged user.

add_action( 'template_redirect', 'wpse230495_author_archive_redirect' );

function wpse230495_author_archive_redirect() {
   if( is_author() && get_the_author_meta('ID') === 1 ) {
       wp_redirect( home_url(), 301 );
       exit;
   }
}

The above code blocks are tested on WordPress 4.5.3 (albeit with only one user in my test install - if your results vary do let me know!).

5
  • Thanks for your answer and help highly appreciated. Before applying the code I'd like to mention it's not the only administrator user however it's the 1st one registered thus you see the USERID=1 the concept is easy we develop the site for our clients add Wordpress White Label Plugin to hide all un-needed menus from the dashboard then hide our username and creates another administrator for the client with hidden menus and deliver it but don't want them to know about our main account which we can call it superadmin but for single site installation. Hope this clarify our request :) – WiTon Nope Jun 24 '16 at 0:08
  • @WiTonNope Got it. In that case, you just need to modify the first block, removing the unset() line and coping the $views['all'] modification to make the same modification to $views['administrator'] :). Glad it helped! – Tim Malone Jun 24 '16 at 0:17
  • Thanks again for your prompt support I'll do the modification and apply the code. I let you know if anything happened, thanks a lot – WiTon Nope Jun 24 '16 at 0:25
  • Thank you for a complete answer. It would be nice though, since the clarification about hiding "a user" was made, to update your answer so one could see the complete code. – marikamitsos Sep 28 '16 at 12:25
  • @marikamitsos Good catch. Done! Hope it helps. – Tim Malone Sep 28 '16 at 23:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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