Solved

How to Hide Users of a Specific Role in the Users List

Posted on 2014-01-16
2
1,528 Views
Last Modified: 2014-01-18
Hi,

I am looking for a way to hide users from a specific role in the users list in the admin. For example, I want to make the sales reps only see client users. I was able to hide the admins with the code below.

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);
  }
}

Open in new window


Another thing that I tried was to do a redirect of the page "users.php" to "users.php?role=client", but that doesn't work either.

Thanks!
0
Comment
Question by:AccurateDesign
2 Comments
 
LVL 11

Accepted Solution

by:
jausions earned 500 total points
ID: 39788128
There are probably different ways to go about it, but in any case don't rely on the user ID being 1 to mean "administrator".

What you should do is use new WordPress "capabilities" and add them to the appropriate user roles. For instance, those new capabilities could show only the users you want (up to you to define whom that would be.) You have to use that approach because you want to "negate" the user list.

You could turn the basic "list_users" capability into only listing users everybody could see, then you add new capabilities to the users or roles who should see more (they will also require the "list_users" capability.)

For instance, create a capability "list_admins" and you assign both "list_admins" and "list_users" to the "Administrator" and "Super-Admin" roles.

In the code hook you then need to look for those new capabilities:
(Note: this is untested code, so sorry in advance for any typos)
add_action( 'pre_user_query', 'yoursite_pre_user_query' );
function yoursite_pre_user_query( $user_search )
{
    global $wpdb;
    $user = wp_get_current_user();
    $user->get_role_caps();
    $where = 'WHERE 1=1';

    // Temporarily remove this hook otherwise we might be stuck in an infinite loop
    remove_action( 'pre_user_query', 'yoursite_pre_user_query' );

    // View adminstrators? (Remember: this is capability defined by you!)
    $view_admins = in_array( 'list_admins', $user->allcaps );
    if ( !$view_admins ) {
        // Get the list of admin IDs
        $args = array(
            'role' => 'Administrator',
        );
        $user_query = new WP_User_Query( $args );
        $admins = $user_query->get_results();

        $admin_ids = array();
        foreach ( $admins as $admin ) {
            $admin_ids[] = $admin->id;
        }

        $where .= ' AND '.$wpdb->users.'.ID NOT IN ('.implode(',', $admin_ids).')';
    }

    // Repeat block above for other capabilities you define,
    // i.e. hide users not everybody should see
    // ...

    // Modify original WP_User_Query
    $user_search->query_where = str_replace(
        'WHERE 1=1',
        $where,
        $user_search->query_where
    );

     // Re-add the hook
    add_action( 'pre_user_query', 'yoursite_pre_user_query' );
}

Open in new window


As said, this is just one suggestion. The downside is we modify the default behavior of the "list_users" capabilities which could have some unforeseen side effects if some plugins expect "list_users" to actually return all the users.

The thing to remember is that Administrators and Super-Admins always have all the roles / capabilities.
0
 

Author Closing Comment

by:AccurateDesign
ID: 39788407
It works perfectly! Also thanks for the heads up about the side effects with plugins. Worst case if weird things starts to happen due to that another alternative might be to use CSS instead to hide the other users
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
The purpose of this video is to demonstrate how to set up basic WordPress SEO. This will be demonstrated using a Windows 8 PC. The plugin used will be WordPress SEO by Yoast. Go to your WordPress login page. This will look like the following: myw…
The purpose of this video is to demonstrate how to set up the permalinks on a WordPress Website. This will be demonstrated using a Windows 8 PC. Go to your WordPress login page. This will look like the following: mywebsite.com/wp-login.php : Go t…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question