Home / Archive / MemberPress: BuddyPress/BuddyBoss – Remove Users From All Groups, Forums, and Topics if Their Subscriptions Expire
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: BuddyPress/BuddyBoss – Remove Users From All Groups, Forums, and Topics if Their Subscriptions Expire

When using MemberPress with BuddyPress/BuddyBoss, and MemberPress BuddyPress integration add-in users can be automatically assigned to BuddyPress Groups, Forums and Topics upon registering for a MemberPress membership.

By default, when the user’s subscription for MemberPress membership expires, the user won’t be removed from the BuddyPress Groups, Forums and Topics.

When this code is added to a website, users will be automatically removed from All BuddyPress Groups, Forums and Topics if their subscriptions expire.

Code Preview
php
<?php
add_action( 'mepr-account-is-inactive' , function ( $txn ) {
  if ( is_plugin_active( 'buddypress/bp-loader.php' ) && $txn instanceof MeprTransaction) {
	  //first we need to make sure that the user isn't active on another subscription
	  $mepr_user = new MeprUser( $txn->user_id );
	  if ( isset( $mepr_user ) && !$mepr_user->is_active() ) { //if we found a user and they are not active, we can continue
		  $paged_groups = groups_get_user_groups( $txn->user_id ); //get all the groups the user belongs to
		  $groups = isset( $paged_groups ) && isset ( $paged_groups['groups'] ) ? $paged_groups['groups'] : false;
		  
		  $forums = bbp_get_user_subscribed_forum_ids ( $txn->user_id ); //get all the forums the user is subscribed to
		  $topics = bbp_get_user_topic_subscriptions( $txn->user_id ); //get all the topics the user is subscribed to
		  if ( isset( $groups ) && $groups && !empty( $groups ) ) {
			  foreach ( $groups as $group ) {
				  BP_Groups_Member::delete($txn->user_id, $group);
			  }
		  }
		  if ( isset( $forums ) && $forums && !empty( $forums ) ) {
			  foreach ( $forums as $forum ) {
				  bbp_remove_user_forum_subscription( $txn->user_id, $forum ); //remove the user from each forum
			  }
		  }
		  
		  if ( isset( $topics ) && $topics && !empty( $topics ) ) {
			  foreach ( $topics as $topic ) {
				  bbp_remove_user_topic_subscription( $txn->user_id, $topic ); //remove the user from each topic
			  }
		  }
	  }
  }
} );

Comments

Add a Comment