(Moderator’s note: Original title was “Remove Admin from User Menu”)
I have created a client administrator role which is essentially an Editor with ability to add/remove users. The article “Editor can create any new user except administrator“ was excellent in helping keep my new client admin role from editing or creating a True admin user.
However what would be ideal is to hide administrators from client admins when they are viewing users. I want them to “believe” that they are the admin of their site but I do not want them to be able to even view my role/user–essentially hiding the “administrator” role from them when they are in the “Users” panel.
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
Hi @Carlos:
Try adding the following to your theme’s functions.php file, or in a .php file within a plugin that you might be writing (which works for WordPress 3.1.x):
add_action('pre_user_query','yoursite_pre_user_query');
function yoursite_pre_user_query($user_search) {
$user = wp_get_current_user();
if ($user->ID!=1) { // Is not administrator, remove administrator
global $wpdb;
$user_search->query_where = str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID<>1",$user_search->query_where);
}
}
If you have WordPress 3.0.x try this instead (since WordPress didn’t add the 'pre_user_query' hook until 3.1):
add_action('pre_user_search','yoursite_pre_user_search');
function yoursite_pre_user_search($user_search) {
$user = wp_get_current_user();
if ($user->ID!=1) { // Is not administrator, remove administrator
global $wpdb;
$user_search->query_where = str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID<>1",$user_search->query_where);
}
}
Method 2
Here’s a mod to MikeSchinkel’s answer that checks if the current user has a role of administrator and if not it only selects users that are subscribers.
add_action('pre_user_query','yoursite_pre_user_query');
function yoursite_pre_user_query($user_search) {
$user = wp_get_current_user();
if ( $user->roles[0] != 'administrator' ) {
global $wpdb;
$user_search->query_where =
str_replace('WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID IN (
SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->prefix}user_level'
AND {$wpdb->usermeta}.meta_value = 0)",
$user_search->query_where
);
}
}
Method 3
User Levels are deprecated, so this method checks against capabilities instead:
/** Hide Administrator From User List **/
function isa_pre_user_query( $user_search ) {
if ( !current_user_can( 'administrator' ) ) { // Is Not Administrator - Remove Administrator
global $wpdb;
$user_search->query_where = str_replace(
'WHERE 1=1',
"WHERE 1=1 AND {$wpdb->users}.ID IN (
SELECT {$wpdb->usermeta}.user_id FROM $wpdb->usermeta
WHERE {$wpdb->usermeta}.meta_key = '{$wpdb->prefix}capabilities'
AND {$wpdb->usermeta}.meta_value NOT LIKE '%administrator%' )",
$user_search->query_where
);
}
}
add_action( 'pre_user_query', 'isa_pre_user_query' );
Method 4
pre_user_query action can be used to alter the user query since WordPress 3.1.0
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0