With BuddyPress Member Types Pro, you can force users to auto join BuddyPress groups while registering account or changing member type. The groups are based on member types.
One issue that you may face is the BuddyPress Member Types Pro won’t remove users from existing groups when their member type changes.
This is due to a few limitation/inconsistency in current implementation of member types in BuddyPress.
- BuddyPress does not provide the details about previous member types of user when firing the bp_set_member_type action.
- Inconsistency in removing member types sometimes. People sometime use bp_remove_member_type() and sometimes bp_set_member_type() with empty string for member type.
- Also, It is possible to have multiple member types for a user but there is no guaranteed way to know if the currently setting member type is the last of the multi member type change.
Due to these issues, we do not include the code to remove users from groups based on member type. It can create a situation where the overall experience is not what an admin will expect.
It is still possible to ensure that the users only belong to groups dictated by their current member type.
You may put this code in your bp-custom.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | /** * Ensure member type dictated groups for users. * * On Member Type change, Ensure that user only belongs to the groups dictated by current member type. * * @param int $user_id numeric user id. * @param string $member_type new member type. * @param boolean $append whether the member type was appended or reset. */ function buddydev_ensure_member_type_only_groups( $user_id, $member_type, $append ) { // valid user? $user = get_user_by( 'id', $user_id ); if ( ! $user ) { return; } if ( ! bp_is_active( 'groups' ) || ! function_exists( 'bpmtp_get_active_member_type_entries' ) ) { return; } $active_types = bpmtp_get_active_member_type_entries(); if ( empty( $active_types ) ) { return; } // Get all current member types for the user. $user_member_types = bp_get_member_type( $user_id, false ); // It could be a result of bp_set_member_type($user_id, '' ); // We should not remove users on this. // Instead go for the remove_member_type action. if ( empty( $user_member_types ) ) { return; } $groups = array(); // find all the groups which the user should have? foreach ( $user_member_types as $user_member_type ) { if ( empty( $active_types[ $user_member_type ] ) ) { continue; } $mt_object = $active_types[ $user_member_type ]; $mtype_groups = get_post_meta( $mt_object->post_id, '_bp_member_type_groups', true ); if ( empty( $mtype_groups ) ) { continue; } $groups = array_merge( $groups, $mtype_groups ); } // find current user groups. $user_groups = groups_get_user_groups( $user_id ); // find all groups where the user should not be a member. $removable_groups = array_diff( $user_groups, $groups ); // Remove. foreach ( $removable_groups as $group_id ) { groups_remove_member( $user_id, $group_id ); } } add_action( 'bp_set_member_type', 'buddydev_ensure_member_type_only_groups', 100, 3 ); |
Please do know that if you have “Multipl member Types” enabled, you will notice some user leaving/joining their already existing groups. This happens since we ensure the member type for each member type individually.