MemberPress + BuddyPress: Add Members to a Group
Adds MemberPress members to a BuddyPress group for their membership when they pay, giving each tier its own private community space.
Opens WPIDE on
Code
if ( ! class_exists( 'MeprUser' ) || ! function_exists( 'groups_join_group' ) ) {
return;
}
// Membership ID => BuddyPress group ID.
$wpide_mepr_bp_groups = array(
123 => 2,
);
add_action( 'mepr-event-transaction-completed', function ( $event ) use ( $wpide_mepr_bp_groups ) {
$txn = $event->get_data();
$group_id = $wpide_mepr_bp_groups[ (int) $txn->product_id ] ?? 0;
if ( $group_id && ! groups_is_user_member( (int) $txn->user_id, $group_id ) ) {
groups_join_group( $group_id, (int) $txn->user_id );
}
} );
Notes
Map membership IDs to BuddyPress group IDs. Members are not removed from groups automatically when they leave.
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.