在BuddyPress中为特定用户角色启用组创建

时间:2014-05-07 07:33:47

标签: php wordpress buddypress

BuddyPress提供“bp_user_can_create_groups”过滤器来限制用户创建组的能力。参考 - http://wordpress.org/support/topic/restrictallow-group-creation-by-user-role?replies=3#post-4617184http://etivite.com/api-hooks/buddypress/trigger/apply_filters/bp_user_can_create_groups/

如何使用此过滤器限制BuddyPress中特定用户角色的组创建?目前,只允许管理员创建组,我也不想让每个用户都能够创建组。

我已将以下代码添加到bp-custom.php但其无法正常工作

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 );

1 个答案:

答案 0 :(得分:0)

您需要编写一个函数并将其附加到'bp_user_can_create_groups'钩子。在您的函数内部确定您是否希望当前用户能够创建组并相应地返回$can_create

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 );