Hide User List in WordPress Admin Panel with Code
Explanation
If you want to keep a specific admin user out of sight in the WordPress admin panel, this code does the trick. Here's how it works:
- Hide a Specific Admin: The code hooks into the user query process to exclude a particular admin from appearing in the user list. You just need to replace 'hidden_admin_username' with the username you want to hide.
- Remove Admin Role from Dropdown: It also removes the Administrator role from the role selection dropdown. This means when you're editing users, you won't see the option to assign the Administrator role, keeping things a bit more secure.
- Prevent Editing of Hidden Admin: Lastly, it ensures that no one can edit, delete, or promote the hidden admin user. This is done by checking if someone is trying to mess with the hidden admin and then blocking those actions.
Remember, this code is all about keeping a specific admin user hidden and secure from prying eyes in the admin panel. Just make sure to update the username in the code to match the admin you want to hide.
Code
<?php
// Hook into the 'pre_user_query' to modify the user query
add_action('pre_user_query', 'wp_dudecom_hide_admin_user_from_list');
/**
* Hide a specific admin user from the user list in the WordPress admin panel.
*
* @param WP_User_Query $user_search The current WP_User_Query instance.
*/
function wp_dudecom_hide_admin_user_from_list($user_search) {
// Check if the current user has the capability to edit users
if (current_user_can('edit_users')) {
global $wpdb;
// Specify the username of the admin you want to hide
$hidden_username = 'hidden_admin_username';
// Modify the query to exclude the specified user
$user_search->query_where .= $wpdb->prepare(" AND {$wpdb->users}.user_login != %s", $hidden_username);
}
}
// Hook into 'editable_roles' to remove the Administrator role from the dropdown
add_filter('editable_roles', 'wp_dudecom_remove_admin_role_from_dropdown');
/**
* Remove the Administrator role from the role dropdown in the WordPress admin panel.
*
* @param array $roles The array of roles.
* @return array Modified array of roles.
*/
function wp_dudecom_remove_admin_role_from_dropdown($roles) {
// Check if the current user has the capability to edit users
if (current_user_can('edit_users')) {
// Remove the Administrator role from the roles array
unset($roles['administrator']);
}
return $roles;
}
// Hook into 'user_has_cap' to prevent editing of the hidden admin user
add_filter('user_has_cap', 'wp_dudecom_prevent_edit_hidden_admin', 10, 4);
/**
* Prevent editing of the hidden admin user by other users.
*
* @param array $allcaps All the capabilities of the user.
* @param array $caps Required capabilities.
* @param array $args Arguments passed to the capability check.
* @param WP_User $user The current user object.
* @return array Modified capabilities.
*/
function wp_dudecom_prevent_edit_hidden_admin($allcaps, $caps, $args, $user) {
// Specify the username of the admin you want to hide
$hidden_username = 'hidden_admin_username';
// Check if the current user is trying to edit the hidden admin user
if (isset($args[2]) && $args[2] === $hidden_username) {
// Remove the capability to edit the hidden admin user
$allcaps['edit_user'] = false;
$allcaps['delete_user'] = false;
$allcaps['promote_user'] = false;
}
return $allcaps;
}
?>
Instructions
To implement the code for hiding a user list in the admin panel, follow these steps:
File Location: You can add the code to your theme's functions.php
file or create a custom plugin file.
Prerequisites: Ensure you have access to your WordPress site's file system and a basic understanding of editing PHP files.
Implementation Steps:
- Access Your WordPress Files: Use an FTP client or your hosting provider's file manager to access your WordPress installation files.
- Locate the File: Navigate to
wp-content/themes/your-active-theme/functions.php
or create a new plugin file inwp-content/plugins/
. - Edit the File: Open the
functions.php
file or your custom plugin file in a text editor. - Paste the Code: Copy the provided code and paste it at the end of the
functions.php
file or in your custom plugin file. - Modify the Username: Replace
'hidden_admin_username'
with the actual username of the admin you want to hide. - Save Changes: Save the changes to the file and upload it back to your server if using an FTP client.
- Verify Implementation: Log in to your WordPress admin panel and navigate to the Users section to ensure the specified admin user is hidden from the list.
If you need assistance with implementation or require more advanced functionality, consider reaching out to wp-dude.com for expert help.