Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save JarrydLong/3129489f599450278c125c22a0e3aaa4 to your computer and use it in GitHub Desktop.
Save JarrydLong/3129489f599450278c125c22a0e3aaa4 to your computer and use it in GitHub Desktop.
<?php //do not copy
// Store the old levels before the change.
function pmpro_store_old_levels( $level_id, $user_id ) {
$old_level_we_want_to_remember = 7;
// Store the current level in a static variable.
$current_level = pmpro_getMembershipLevelForUser( $user_id );
if ( ! empty( $current_level ) && $current_level === $old_level_we_want_to_remember ) {
// Save in a global so the next function can access it.
$GLOBALS['stored_old_level'] = $current_level;
}
}
add_action( 'pmpro_before_change_membership_level', 'pmpro_store_old_levels', 10, 2 );
// Use the stored level after the change.
function pmpro_after_change_membership_level_default_level( $level_id, $user_id ) {
$level_we_want_to_give_back = 7;
// If user is cancelling (level 0), and we have stored data
if ( $level_id == 0 && ! empty( $GLOBALS['stored_old_level'] ) ) {
$old_level = $GLOBALS['stored_old_level'];
// Assign default level
pmpro_changeMembershipLevel( $level_we_want_to_give_back, $user_id );
}
}
add_action( 'pmpro_after_change_membership_level', 'pmpro_after_change_membership_level_default_level', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment