Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Remove role and assign subscriber role to the user or membership cancel
<?php
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' );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment