Skip to content

Instantly share code, notes, and snippets.

@ipokkel
Created January 4, 2021 12:51
Show Gist options
  • Save ipokkel/f6a4e868919b038401d601bfc0e35c98 to your computer and use it in GitHub Desktop.
Save ipokkel/f6a4e868919b038401d601bfc0e35c98 to your computer and use it in GitHub Desktop.
Customize the reason for cancelling per language locale #pmpror4c #pmpro-reason-for-cancelling
<?php
/**
* This recipe customizes the reason for cancelling per language locale.
*
* 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_multilingual( $body, $email ) {
// Bail if Reason for cancellation is not active
if ( ! function_exists( 'pmpror4c_init' ) ) {
return $body;
}
if ( ! empty( $_REQUEST['reason'] ) ) {
$reason = wp_unslash( sanitize_text_field( $_REQUEST['reason'] ) );
} else {
$reason = __( 'N/A', 'pmpro-reason-for-cancelling' );
}
global $locale;
switch ( $locale ) {
case 'fr_FR':
$needle = 'a été résiliée.</p>';
$replacement = 'a été résiliée.</p><p>Motif: ' . $reason . '</p>';
break;
default:
$needle = 'has been cancelled.</p>';
$replacement = 'has been cancelled.</p><p>Reason: ' . $reason . '</p>';
}
// replace in standard templates
if ( $email->template == 'cancel' || $email->template == 'cancel_admin' ) {
$body = str_replace( $needle, $replacement, $body );
}
// or replace in custom template
$body = str_replace( '!!reason!!', $reason, $body );
return $body;
}
function my_pmpror4c_email_body_function_switch() {
// Bail if Reason for cancellation is not active
if ( ! function_exists( 'pmpror4c_init' ) ) {
return;
}
// Switch with custom function.
if ( has_action( 'pmpro_email_body', 'pmpror4c_pmpro_email_body' ) ) {
remove_action( 'pmpro_email_body', 'pmpror4c_pmpro_email_body' );
add_action( 'pmpro_email_body', 'my_pmpror4c_pmpro_email_body_multilingual', 10, 2 );
}
}
add_action( 'init', 'my_pmpror4c_email_body_function_switch' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment