简体   繁体   中英

Enable group creation for specific user role in BuddyPress

BuddyPress provides the "bp_user_can_create_groups" filter to restrict a user's ability to create groups. Reference - http://wordpress.org/support/topic/restrictallow-group-creation-by-user-role?replies=3#post-4617184 and http://etivite.com/api-hooks/buddypress/trigger/apply_filters/bp_user_can_create_groups/

How can I use this filter to restrict group creation for a particular user role in BuddyPress? Currently, only admins are allowed to create groups and I also dont want to allow every user to be able to create groups.

I have added the following code to bp-custom.php but its not working

function create_groups1( $can_create, $restricted=false ) {
// maybe we don't want to override if it's restricted?
if ( ! $restricted ){
    // get the logged in user's ID
    $user_ID = get_current_user_id();
    // some logic to determine if the current user can create a group
    $user1 = new WP_User( $user_ID );

    if ( !empty( $user1->roles ) && is_array( $user1->roles ) ) {
            $r=$user1->roles[0];
    }
    if (current_user_can('read')) {
        $r="subscriber";
    }
    if ( $r == "subscriber" || $r == "participant" ){
        // we will return this allowing them to create groups
        $can_create = true;
    }
}
return $can_create;
}
add_filter( 'bp_user_can_create_groups', 'create_groups1', 10, 2 );

You need to write a function and attach it to the 'bp_user_can_create_groups' hook. Inside your function determine if you want the current user to be able to create groups and return $can_create accordingly.

function bp_user_can_create_groups( $can_create, $restricted=false ){
    // maybe we don't want to override if it's restricted?
    if ( ! $restricted ){
        // get the logged in user's ID
        $user_ID = get_current_user_id();
        // some logic to determine if the current user can create a group
        if ( user_can_create_group( $user_ID ) ){
            // we will return this allowing them to create groups
            $can_create = true;
        }
    }
    return $can_create;
}
add_filter( 'bp_user_can_create_groups', 'bp_user_can_create_groups', 10, 2 );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM