Last active
July 21, 2023 13:42
-
-
Save ipokkel/311725d603a36d4fbe45244e4932e690 to your computer and use it in GitHub Desktop.
Custom membership expiring email subject and template per membership level.
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 | |
/** | |
* This recipe sets a custom membership expiring email template and subject line per level. | |
* | |
* In this example recipe we created a custom email template for level ID's 1 & 2 | |
* in the "email" folder inside our customization plugin's directory. | |
* | |
* Note: This recipe and the required email template folders need to be added | |
* to a custom plugin and will not work from a third-party plugin that adds | |
* php customization recipes to the site. | |
* | |
* You can add this recipe to your site by creating a custom plugin. | |
* 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_email_custom_membership_expiring_template_per_level( $email ) { | |
// Is this a checkout email for the member? | |
if ( strpos( $email->template, 'membership_expiring' ) !== false && strpos( $email->template, 'admin' ) === false ) { | |
// which level is this email for? | |
$level_for_email = $email->data['membership_id']; | |
switch ( $level_for_email ) { | |
case '1': | |
$email->subject = 'Custom subject line for level 1'; | |
// Be sure to create the checkout_level_1.html email in the file location here. | |
$email->body = file_get_contents( dirname( __FILE__ ) . '/email/membership_expiring_level_1.html' ); | |
break; | |
case '2': | |
$email->subject = 'Custom subject line for level 2'; | |
// Be sure to create the checkout_level_2.html email in the file location here. | |
$email->body = file_get_contents( dirname( __FILE__ ) . '/email/membership_expiring_level_2.html' ); | |
break; | |
} | |
} | |
// Let's check for any remaining email variables and replace them with the data. | |
if ( is_array( $email->data ) ) { | |
foreach ( $email->data as $key => $value ) { | |
if ( 'body' != $key ) { | |
$email->body = str_replace( '!!' . $key . '!!', $value, $email->body ); | |
$email->subject = str_replace( '!!' . $key . '!!', $value, $email->subject ); | |
} | |
} | |
} | |
return $email; | |
} | |
// Set filter at a later priority than 10 | |
add_filter( 'pmpro_email_filter', 'my_pmpro_email_custom_membership_expiring_template_per_level', 20, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
For a custom checkout template per level see: https://gist.github.com/ipokkel/f2525f540cc7e5bf88afc2cc83278d68
For a custom membership expired template per level see: https://gist.github.com/ipokkel/2563d3e795a5cb208b7f8c09b693d294
For a custom cancel email template per level see: https://gist.github.com/ipokkel/9054d94a7d7ab45084c188bf250c7fe3