0

im trying to modify the Wordpress capability 'edit_users' for my plugin. In my case some User Roles should have the ability to modify some other Users with an specific Role BUT not every other Users.

So I just add the capability 'edit_users' to my custom Role 'Primary Trainer'. Now he can edit every User. I can use the 'edit_user' cap to check every shown Users.

Then i filtered this capability by:

add_filter( 'map_meta_cap',array($this,'sa_classbook_map_meta_cap'),10,4); 
function sa_classbook_map_meta_cap( $caps, $cap, $user_id, $args ) {

    switch( $cap ){
        //Some Roles can only edit some Profiles
        //Primary Trainer and Secondary Trainers are allowed to change Data of Participants but they cant change other Trainers Data
        case 'edit_user':
            if( isset($args[0]) && $args[0] == $user_id )
                break;
            elseif( !isset($args[0]) )
                $caps[] = 'do_not_allow';
            $other = new WP_User( absint($args[0]) );
            //If the Current User is not the Admin
            if(!current_user_can('administrator')){
                //If the shown Profile is an Primary Trainer, an Secondary Trainer or the Admin it should'nt be editable
                if(!in_array( 'sa_classbook_participant', (array) $other->roles)){
                    $caps[] = 'do_not_allow';
                }
            }
            break;
        default:
            break;
    }
    return $caps;
}

For the Backend it works perfectly see picture: enter image description here

But now comes Buddy Press:

Buddypress give the ability to edit users information in Frontend. But the thing is my filter wont be called. There seems only the ability to give the capability to edit every user or none.

Heres a Picture so you see what i mean: enter image description here

Do you have an idea ? Or maybe a better solution ?

0

Ok I got an solution for my case. Maybe a bit confusing but I did'nt saw anothere possibility to do this. If you know a better way let me know.

First I hide the Edit Item in the Admin Menu:

add_action( 'admin_bar_menu', array($this,'sa_classbook_remove_admin_bar_items'), 999 );
function sa_classbook_remove_admin_bar_items(){
        global $wp_admin_bar;

        $wp_admin_bar->remove_node('user-admin');
    }

Then I check the roles manually:

add_action( 'bp_actions', array($this,'sa_classbook_bp_remove_nav_tabs' ));
function sa_classbook_bp_remove_nav_tabs(){
        $current_user = get_user_by("ID",bp_displayed_user_id());
        //Only do this if the displayed Profile is not the own
        if(bp_displayed_user_id() !== get_current_user_id()) {
            //If the Current User is not the Admin
            if (!current_user_can('administrator')) {
                if (!in_array('sa_classbook_participant', (array)$current_user->roles)) {
                    //Remove settings
                    bp_core_remove_nav_item('notifications');
                    bp_core_remove_nav_item('settings');

                    bp_core_remove_subnav_item('profile', 'edit');
                    bp_core_remove_subnav_item('profile', 'change-avatar');
                    bp_core_remove_subnav_item('profile', 'change-cover-image');


                }
            }
        }
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.