Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save kimcoleman/7d392c61d8743b29ac272aa4e29ad018 to your computer and use it in GitHub Desktop.
Save kimcoleman/7d392c61d8743b29ac272aa4e29ad018 to your computer and use it in GitHub Desktop.
Place a PMPro member in another level when they expire and set an expiration date.
<?php
/**
* When users expire (and the system changes them to membership level 0) we give them another downgraded level.
*/
function my_pmpro_membership_post_membership_expiry( $user_id, $level_id ) {
// Set this to the id of the level you want to give members when they cancel.
$downgrade_level_1 = array(
'user_id' => $user_id,
'membership_id' => 1,
'code_id' => '',
'initial_payment' => '',
'billing_amount' => '',
'cycle_number' => '',
'cycle_period' => '',
'billing_limit' => '',
'trial_amount' => '',
'trial_limit' => '',
'startdate' => "'" . current_time('mysql') . "'",
'enddate' => date("Y-m-d", strtotime( '+ 1 Year' ) ),
);
// If we see this global set, then another gist is planning to give the user their level back.
global $pmpro_next_payment_timestamp;
if ( ! empty( $pmpro_next_payment_timestamp ) ) {
return;
}
// Are they cancelling?
if ( $level_id == 0 ) {
// Check if they are cancelling from level $cancel_level_id.
global $wpdb;
$last_level_id = $wpdb->get_var( "SELECT membership_id FROM $wpdb->pmpro_memberships_users WHERE user_id = '" . $user_id . "' ORDER BY id DESC" );
if( $last_level_id == 1 ) {
return; // Let them cancel.
}
// Otherwise give them level $cancel_level_id instead.
pmpro_changeMembershipLevel( $downgrade_level_1, $user_id );
}
}
add_filter( 'pmpro_membership_post_membership_expiry', 'my_pmpro_membership_post_membership_expiry', 10, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment