Skip to content

Instantly share code, notes, and snippets.

@andrewlimaza
Last active October 31, 2023 06:39
Show Gist options
  • Save andrewlimaza/0adf39db98cf51603f2b7415c31e99e5 to your computer and use it in GitHub Desktop.
Save andrewlimaza/0adf39db98cf51603f2b7415c31e99e5 to your computer and use it in GitHub Desktop.
Change user's membership level only when a user expires from a specific level.
<?php
/**
* Change membership level within the Paid Memberships Pro expiration code (this does not affect manual cancellations at all).
* User's will still be able to cancel their membership level after their expiration level change or before.
* Please read through each line of code and adjust the level ID's found inside pmpro_changeMembershipLevel() function accordingly.
* Add this code to your PMPro Customizations Plugin - https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/
*
* www.paidmembershipspro.com
*/
function pmpro_set_default_level_only_when_expiring( $user_id, $level_id ) {
global $wpdb;
$SQL = "SELECT membership_id FROM $wpdb->pmpro_memberships_users WHERE user_id = $user_id AND status='expired' ORDER BY id DESC LIMIT 1";
$results = $wpdb->get_results( $SQL, OBJECT );
$expired_level = $results[0];
$old_level_id = (int) $expired_level->membership_id;
// If there is no previous level they are expiring from, let's just default to level 1.
if ( ! empty( $old_level_id ) ) {
// check what level they just expired from.
if ( 3 == $old_level_id ) {
pmpro_changeMembershipLevel( 2, $user_id );
}
if ( 5 == $old_level_id ) {
pmpro_changeMembershipLevel( 3, $user_id );
}
} else {
pmpro_changeMembershipLevel( 1, $user_id ); // If no previous level is found, default to level 1.
}
return $user_id;
}
add_filter( 'pmpro_membership_post_membership_expiry', 'pmpro_set_default_level_only_when_expiring', 10, 2 );
@zduni100
Copy link

zduni100 commented Jun 24, 2022

Unfortunately your code does not work. Any solution?

@andrewlimaza
Copy link
Author

For assistance, please open a support ticket on www.paidmembershipspro.com/support

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment