Skip to content

Instantly share code, notes, and snippets.

@bappi-d-great
Created August 11, 2015 10:41
Show Gist options
  • Save bappi-d-great/cf1c337e5b7cdba77eb3 to your computer and use it in GitHub Desktop.
Save bappi-d-great/cf1c337e5b7cdba77eb3 to your computer and use it in GitHub Desktop.
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