$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>plugin development - Buddy Press restrict the capability to edit users|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

plugin development - Buddy Press restrict the capability to edit users

matteradmin7PV0评论

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:

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:

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

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:

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:

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

Share Improve this question edited Nov 22, 2018 at 9:11 DevNik asked Nov 22, 2018 at 9:06 DevNikDevNik 1032 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 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');


                }
            }
        }
    }
Post a comment

comment list (0)

  1. No comments so far