Home / Archive / MemberPress: Change the Member’s User Role After a Specific Membership Subscription Cancelation
Duplicate Snippet

Embed Snippet on Your Site

MemberPress: Change the Member’s User Role After a Specific Membership Subscription Cancelation

The code automatically changes the member's user role once the subscription is canceled. This will be applied only for canceled subscriptions to the specific membership.

The code needs to be adjusted by changing the dummy membership ID of 123 within the code with the ID of the actual membership(s). The code needs to be adjusted by changing the membership ID on the following line:

if( 123 != ( int ) $subscription->product_id ) {

Code Preview
php
<?php
function mepr_change_role_canceled_subscription( $event ) {
  $subscription = $event->get_data();
  if( 123 != ( int ) $subscription->product_id ) { 
	  return; 
  }
  $user = $subscription->user();
  $wp_user = get_user_by( 'id', $user->ID );
  if( !$wp_user ) { 
	  return; 
  }
  // Remove role
  $wp_user->remove_role( 'subscriber' );
  // Add role
  $wp_user->add_role( 'editor' );
}
add_action( 'mepr-event-subscription-stopped', 'mepr_change_role_canceled_subscription' );

Comments

Add a Comment