Brad Starnes February 2016

How to limit WordPress users from accessing other users

I'm trying to limit certain users from seeing certain other users within the database.

We have a company that allows banks to register within their site with a membership. That company can then add their own employees into the site to access E-learnings, conference tickets, etc.

What I am trying to do is make it so that Company A can create/edit/delete their own employees, but they can't access or see the employees of Company B.

I found the following code, but I'm not the greatest in php and the person who posted the code on the wordpress.org blog stated it wasn't complete.

How can I make this do what I want?

// Custom User Roles to limit capabilities to view other users within other inst. 
function admin_users_filter( $query ){

    global $pagenow,$wp_query,$user_id;

    if ( is_admin() && $pagenow=='users.php' && !current_user_can('manage_options') ) {
        $editor_user_id = get_current_user_id();
        $affiliate = get_user_meta($editor_user_id, 'affiliate', true);
        $query->search_term = $affiliate;
        global $wpdb;
        if (!is_null($query->search_term)) {
            $query->query_from .= " INNER JOIN {$wpdb->usermeta} ON " . "{$wpdb->users}.ID={$wpdb->usermeta}.user_id AND " . "{$wpdb->usermeta}.meta_key='affiliate' AND "."{$wpdb->usermeta}.meta_value    LIKE '%{$query->search_term}%'";
        }
    }
}
add_filter( 'pre_user_query', 'admin_users_filter' ); 

Answers


markratledge February 2016

We have a company that allows banks to register within their site with a membership....

If you're dealing with banking info (and not even financial data), employee data, course registration, tickets, etc., you're absolutely foolish to pull a random php function from the web and try to integrate it into a WordPress theme and expect the results to work and - most importantly - be secure. Even for use simply as a demo.

Do yourself and your work and your job a favor and fork out a little bit of money for one of many commercially available membership plugins for WordPress. They are reasonably secure, offer features you will find you need once you get into this, and offer support. Google or look at https://wordpress.org/plugins/search.php?q=membership

Post Status

Asked in February 2016
Viewed 1,829 times
Voted 9
Answered 1 times

Search




Leave an answer