Solved

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

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
This video teaches viewers how to create their own website using cPanel and Wordpress. Tutorial walks users through how to set up their own domain name from tools like Domain Registrar, Hosting Account, and Wordpress. More specifically, the order in…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

771 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

13 Experts available now in Live!

Get 1:1 Help Now