Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Youzify - Restrict BuddyPress Groups Access by Group Type
<?php
/**
* Youzify - Restrict BuddyPress Groups Access by Group Type
* */
function yzc_set_restriction_by_group_type() {
$restriction_roles = array(
'subscriber' => array(
'nature' => 5,
'sport' => 1
),
'administrator' => array(
'nature' => 5,
'sport' => 1
),
);
$group_id = isset( $_POST['gid'] ) ? $_POST['gid'] : '';
if ( empty( $group_id ) ) {
$group_id = isset( $_POST['item_id'] ) ? $_POST['item_id'] : '';
}
$group_types = bp_groups_get_group_type( $group_id, false );
if ( empty( $group_types ) ) {
return;
}
$current_user_id = bp_loggedin_user_id();
// Get User Data.
$user = get_userdata( $current_user_id );
foreach ( $user->roles as $role ) {
if ( isset( $restriction_roles[ $role ] ) ) {
foreach ($group_types as $group_type ) {
if ( isset( $restriction_roles[ $role ][ $group_type ] ) ) {
$groups = groups_get_groups(
array(
'user_id' => $current_user_id,
'group_type' => $group_type,
'fields' => 'ids'
)
);
if ( $groups['total'] > $restriction_roles[ $role ][ $group_type ] ) {
$tax = bp_groups_get_group_type_object( 'nature' );
return sprintf( __( 'Sorry, you cannot access more than %d %s groups' ), $restriction_roles[ $role ][ $group_type ], $tax->labels['singular_name'] );
}
}
}
}
}
return;
}
add_filter( 'yzmr_groups_join_group', 'yzc_set_restriction_by_group_type' );
add_filter( 'yzmr_groups_request_membership', 'yzc_set_restriction_by_group_type' );
add_filter( 'yzmr_groups_accept_invite', 'yzc_set_restriction_by_group_type' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment