Created
September 15, 2025 12:36
-
-
Save JarrydLong/213f035d40f9f3a54757cd3fe7f089f1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php //do not copy | |
| /** | |
| * This recipe will show either the last level a member had, or if they're a new member. | |
| * | |
| * Use the !!old_level_name!! variable in an email template. | |
| * | |
| * You can add this recipe to your site by creating a custom plugin | |
| * or using the Code Snippets plugin available for free in the WordPress repository. | |
| * Read this companion article for step-by-step directions on either method. | |
| * https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
| */ | |
| function my_pmpro_add_old_level_name_to_email_filter( $email ){ | |
| //Default to 'New Member' if no old level found | |
| $email->data['old_level_name'] = "New Member"; | |
| $user = get_user_by( 'email', $email->data['user_email'] ); | |
| $user_id = $user->ID ?? null; | |
| $order_id = $email->data['order_id'] ?? null; | |
| if ( empty( $user_id ) || empty( $order_id ) ) { | |
| return $email; | |
| } | |
| // Get full membership history (including inactive/old levels) | |
| $levels = pmpro_getMembershipLevelsForUser( $user_id, true ); | |
| if ( empty( $levels ) || ! is_array( $levels ) ) { | |
| return $email; | |
| } | |
| // Get the last *inactive* level (exclude current active one if present) | |
| $old_levels = array_filter( $levels, function( $level ) { | |
| return ( empty( $level->enddate ) || $level->enddate < current_time( 'mysql' ) ); | |
| }); | |
| // Sort by enddate descending so last ended is first | |
| usort( $old_levels, function( $a, $b ) { | |
| return strtotime( $b->enddate ) <=> strtotime( $a->enddate ); | |
| }); | |
| if ( ! empty( $old_levels ) ) { | |
| $last_old_level = reset( $old_levels ); | |
| $email->data['old_level_name'] = "Previous Plan: " . $last_old_level->name; | |
| } | |
| return $email; | |
| } | |
| add_filter( 'pmpro_email_filter', 'my_pmpro_add_old_level_name_to_email_filter', 10, 1 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment