Solved

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

Posted on 2014-01-16
2
1,423 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In Part I (http://www.experts-exchange.com/Web_Development/Blogs/WordPress/A_8410-Getting-Started-In-WordPress-Part-I.html), I introduced you to the powerful WordPress backend, the WordPress administrative Dashboard.  In Part II, I will introduce yo…
So you have coded your own WordPress plugin and now you want to allow users to upload images to a folder in the plugin folder rather than the default media location? Follow along and this article will show you how to do just that!
This video teaches users how to migrate an existing Wordpress website to a new domain.
The purpose of this video is to demonstrate how to prevent comment spam on a WordPress Website. This will be demonstrated using a Windows 8 PC. Plugin Akismet will be used. Go to your WordPress login page. This will look like the following: myw…

919 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now