Want to add users as BuddyPress group admin based on their member types? It’s easy.
BuddyPress Member Types Pro plugin takes care of adding user as group member. All we need to do is upgrade the membership as group admin or moderator.
The code works by taking a map of member type to group role. You only need to specify the map for the group admin and moderator role.
Here is the code.
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 | /** * Promote user as the group admin for the given member types. * * @param int $user_id user id whose member type is changing. * @param string $member_type new member type name. * @param bool $append is it appending or resetting. */ function buddydev_promote_as_group_admin_on_member_types_update( $user_id, $member_type, $append ) { // Update with your member types. // we don't do anything on member type reset or if our plugin is not active. if ( empty( $member_type ) || ! function_exists( 'bpmtp_member_types_pro' ) ) { return; } // use member type slug(unique name) here, It is not label. // member type to role map, use member_type_name=>group_role(admin|mod). $member_types_role_map = array( 'advisor' => 'admin',// users will be added as group admin. 'officer' => 'mod', // All users with this member type will be added as the moderator to their respective group. ); if ( ! isset( $member_types_role_map[ $member_type ] ) ) { return;// member was already added by member types pro and it is not our member type. } $role = $member_types_role_map[ $member_type ]; // if we are here, let us upgrade the membership. // Get active types. $active_types = bpmtp_get_active_member_type_entries(); if ( empty( $member_type ) || empty( $active_types ) || empty( $active_types[ $member_type ] ) ) { return; } $mt_object = $active_types[ $member_type ]; $groups = get_post_meta( $mt_object->post_id, '_bp_member_type_groups', true ); if ( empty( $groups ) ) { return; } $user = get_user_by( 'id', $user_id ); if ( ! $user ) { return; } // Now add the one/more roles to the user. foreach ( $groups as $group_id ) { // upgrade role to admin/mod. groups_promote_member( $user_id, $group_id, $role ); } } add_action( 'bp_set_member_type', 'buddydev_promote_as_group_admin_on_member_types_update', 11, 3 ); |
It fetches the list of group associated with a member type and upgrades the user role as group admin/mod based on your preference.