Disallow Users Roles From Editable Roles List in wordpress
July 17, 2014

Hide administrator usders from user list where logged in usre is not administrator

I was working for my client and the task was to hide users from the user list having specific user role i.e. administrator. Here is the snippet that I write with the help of Mr. Google 😛
We simple need to query inside the query that is the basic trick here is my code snippets (user functions.php to apply this to your website)


function hide_administrator_from_user_list($user_search) {
$user = wp_get_current_user();
if (!current_user_can('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
);
}

AND {$wpdb->usermeta}.meta_value NOT LIKE ‘%administrator%’)”,  by modifying this line we can also putt the another role check with or condition
After putting this snippets to functions.php  we need to hide the user count above the user list with user role i.e Administrator(1) etc. It is also pretty simple just add an action to wp_head and write a css code to display it none
Here is my snippet

function hide_user_count_of_administrator(){
echo "<style>.subsubsub .administrator{display:none;}</style>";
}

add_action(‘admin_head’,’hide_user_count_of_administrator’);
where . subsubsub is the main root class to ul li of the counter and administrator is the class of li that role is associated. That’s all we have done

Stay Scuss any feedbak and comments are welcome.