Change User role When Membership Ends

I am following this thread to change a user's role back to "Subscriber" when their membership ends: https://premium.wpmudev.org/forums/topic/auto-assign-wp-role-at-registration-based-on-membership-level. I am using the following code, but it doesn't seem to work.

add_action( 'ms_model_event', 'my_event_handler', 10, 2 );

/**
 * Handles an event and process the correct communication if required.
 *
 * @param  MS_Model_Event $event The event that is processed.
 * @param  mixed $data The data passed to the event handler.
 */
function my_event_handler( $event, $data ) {
	$member = false;
	$subscription = false;
	$membership = false;

	switch ( $event->type ) {
		case MS_Model_Event::TYPE_MS_CANCELED:
			// A membership was cancelled - either by Admin or by the member.
			// No more payments will be made but member has access until current period ends.
			$subscription = $data;
			$membership = $data->get_membership();
			$member = $subscription->get_member();
			assign_default_role( $member->id );
			break;
		case MS_Model_Event::TYPE_MS_DEACTIVATED:
			// A membership was permanently deactivated. Member has no access anymore.
			$subscription = $data;
			$membership = $data->get_membership();
			$member = $subscription->get_member();
			assign_default_role( $member->id );
			break;
	}

}
function assign_default_role( $user_id = 0 ){
	$user = new WP_User( $user_id );
	$user->set_role( 'subscriber' );
}

Is there an update to get this working with Memberships 2?

Thanks!

  • Rupok

    Hi Patrick

    I hope you had a wonderful day.

    I've just tried the following code by creating a MU Plugin and it's working on my end. Can you please check and confirm again? For creating a mu plugin, create a folder named "mu-plugins" inside your "wp-content" folder. Then inside that "mu-plugins" folder, create a file with any name and with a .php extension. Now paste the code inside that file and save.

    For getting more idea on mu plugins, you can go through this article: https://premium.wpmudev.org/manuals/wpmu-manual-2/using-mu-plugins/

    add_action( 'ms_model_event', 'my_event_handler', 10, 2 );
    
    /**
     * Handles an event and process the correct communication if required.
     *
     * @param  MS_Model_Event $event The event that is processed.
     * @param  mixed $data The data passed to the event handler.
     */
    function my_event_handler( $event, $data ) {
      $member = false;
      $subscription = false;
      $membership = false;
    
      switch ( $event->type ) {
        case MS_Model_Event::TYPE_MS_CANCELED:
          // A membership was cancelled - either by Admin or by the member.
          // No more payments will be made but member has access until current period ends.
          $subscription = $data;
          $membership = $data->get_membership();
          $member = $subscription->get_member();
          assign_default_role( $member->id );
          break;
        case MS_Model_Event::TYPE_MS_DEACTIVATED:
          // A membership was permanently deactivated. Member has no access anymore.
          $subscription = $data;
          $membership = $data->get_membership();
          $member = $subscription->get_member();
          assign_default_role( $member->id );
          break;
      }
    
    }
    function assign_default_role( $user_id = 0 ){
      $user = new WP_User( $user_id );
      $user->set_role( 'editor' );
    }

    Have a nice day. Cheers!
    Rupok

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.