How To Hide Administrator From User List In WordPress?

Few days back one of our readers asked us how he can hide the admin role from his website’s ‘All Users’ list. You can easily do so, all you need to do is open your theme’s function.php file and add the following hack. The hack simply hides all admins of your website from the eyes of others users. The plugin works out of box, by default Users page is hidden from other users roles but if you are using any plugin for editing user capabilities and provided other users roles the right to add/edit users, see user list etc then also the plugin will be keep admins hidden from the eyes of other users.

For Hiding Administrator From User List In WordPress:

Open your theme’s function.php file and copy-paste the following code:

/** Hide Administrator From User List **/
function isa_pre_user_query($user_search) {
  $user = wp_get_current_user();
  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%')", 

Photo by Titanas

Photo by jST.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.