BuddyPress: Let Only Certain Roles Create Groups
Restricts creating BuddyPress groups to the roles you choose, such as editors or paying members, while everyone can still join groups.
Opens WPIDE on
Code
if ( ! function_exists( 'buddypress' ) ) {
return;
}
// Roles allowed to create groups.
$wpide_bp_group_creators = array( 'editor', 'author' );
add_filter( 'bp_user_can_create_groups', function ( $can_create ) use ( $wpide_bp_group_creators ) {
if ( ! is_user_logged_in() ) {
return false;
}
if ( bp_current_user_can( 'bp_moderate' ) ) {
return true;
}
return $can_create && (bool) array_intersect( $wpide_bp_group_creators, (array) wp_get_current_user()->roles );
} );
Notes
Administrators can always create groups.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.