Created
April 12, 2021 08:40
-
-
Save ipokkel/417d0abe3b53017ad0ea4d33326a43b2 to your computer and use it in GitHub Desktop.
Add the reason for cancelling to the email body only when the shortcode !!reason!! was not used. #pmpro-reason-for-cancelling
This file contains 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 adds the reason for cancelling in the default cancel email templates | |
* if the email shortcode !!reason!! is not in the 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_pmpror4c_pmpro_email_body( $body, $email ) { | |
if ( ! empty( $_REQUEST['reason'] ) ) { | |
$reason = wp_unslash( sanitize_text_field( $_REQUEST['reason'] ) ); | |
} else { | |
$reason = __( 'N/A', 'pmpro-reason-for-cancelling' ); | |
} | |
// replace in standard templates if !!reason!! shortcode not present in template | |
if ( $email->template == 'cancel' || $email->template == 'cancel_admin' ) { | |
if ( ! strpos( $body, '!!reason!!' ) ) { | |
$body = str_replace( 'has been cancelled.</p>', 'has been cancelled.</p><p>Reason: ' . $reason . '</p>', $body ); | |
} | |
} | |
// or replace in custom template | |
$body = str_replace( '!!reason!!', $reason, $body ); | |
return $body; | |
} | |
function my_init_pmpror4c_pmpro_email_body_action() { | |
remove_action( 'pmpro_email_body', 'pmpror4c_pmpro_email_body', 10, 2 ); | |
add_action( 'pmpro_email_body', 'my_pmpror4c_pmpro_email_body', 10, 2 ); | |
} | |
add_action( 'init', 'my_init_pmpror4c_pmpro_email_body_action' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment