How to hide user account from WordPress user list with code

In a multi-author WordPress website, it may be necessary to hide a particular user account from the user list in the WordPress admin. This can be useful for security purposes or to keep the list of users private and manageable. In this tutorial, we’ll show you how to hide a particular user account from the WordPress user list using simple code, specifically through the use of the “pre_user_query” filter in WordPress. We’ll also cover alternative methods for hiding a particular user from the list, as well as the importance of testing the changes on a development environment before implementing them on a live site. With this simple solution, you can keep your user account safe from other users’ eyes and maintain control over who can see them. So, let’s get started! 

Understanding the “pre_user_query” filter in WordPress

WordPress actions and filters are a powerful tool that allows you to modify and customize the behavior of your WordPress site. The “pre_user_query” filter is specifically used to modify the user query used to display the list of users in the WordPress admin. This hook fires after the wpdb WP_User_Query has been parsed, and before the query is executed.

wordpress hide user from -list

By using the “pre_user_query” filter, you can easily exclude a particular user (this can be an admin user or any user role) from the list of users displayed in the WordPress admin. To do this, you would check the current user’s ID and exclude the user from the list if necessary.

Here is an example of the code that could be used to hide a particular administrator account from the WordPress user list using the “pre_user_query” filter:

 

 

This WordPress action hook modifies the default user search query. The add_action() function connects our yoursite_pre_user_query function to the pre_user_query action hook, which is triggered before a user query is executed.

The yoursite_pre_user_query function takes the user search query object as its argument. The function first retrieves the current user’s login username using the $current_user global object. If the username is not “hide-this-user” the function modifies the query_where property of the user search query object by replacing the default “WHERE 1=1” clause with a modified version that excludes the user with the login username “hide-this-user”.

In summary, this code modifies the default user search query to exclude the user with the login username “hide-this-user”.

Using the WordPress Code Snippets plugin

The WordPress Code Snippets plugin is a collection of code snippets that can be used to add custom functionality to your WordPress site. Without the Code Snippets plugin, to use a code snippet, you would typically create a custom plugin and paste the code into the plugin file.

In our case, to hide a particular admin account from the WordPress user list using code, you would create a custom snippet with the code as shown in the example above. This will allow you to hide the particular admin account from the user list without making any changes to your theme’s functions.php file.

Here are the steps for creating a custom plugin with a code snippet to hide a particular admin account from the WordPress user list:

  1. Log in to your WordPress admin dashboard.
  2. Go to Plugins > Add New.
  3. Click on the “Search plugins…” field.
  4. Type “Code Snippets” and press enter.
  5. Locate the plugin in the search results and press “Install Now”.
  6. Activate the plugin.

Testing the Changes on a Staging Site or Development Environment

Before making any changes to your live WordPress site, it is important to test the changes on a staging site or development environment. This will allow you to ensure that the changes work as expected and that there are no unintended consequences.

To test the changes, you would simply repeat the steps outlined in the previous sections on a staging site or development environment. Once you are satisfied with the changes, you can then implement them on your live WordPress site.

Alternative Methods for Hiding a Particular User from the List

In addition to using the “pre_user_query” filter and a custom plugin, there are other methods for hiding a particular user from the list of users in the WordPress admin.

One alternative method is to use a plugin specifically designed for this purpose. There are several plugins available in the WordPress plugin repository that allow you to hide specific users from the list of users in the WordPress admin. Also there are situation when you might consider deleting the admin account instead.

Another alternative method is to add custom code to your theme’s functions.php file. However, this method is not recommended, as it involves making changes to the core files of your WordPress site. If you are not familiar with working with WordPress code, it is best to use the “pre_user_query” filter or a plugin specifically designed for this purpose.

 

Conclusion

Hiding a particular admin account from the WordPress user list in the admin can be a useful tool for keeping your username list organized and improving WordPress security and access. By using the “pre_user_query” filter, a custom plugin, or a plugin specifically designed for this purpose, you can easily hide a particular username from the list of users in the WordPress admin.