Created
March 12, 2024 07:12
-
-
Save ipokkel/625748c15775d1ec082db5b8c9ab5042 to your computer and use it in GitHub Desktop.
Set a custom PMPro Email subject per locale.
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 | |
/** | |
* Change the email subject based on the user's 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 pmpro_email_change_subject_based_on_locale( $subject, $email ) { | |
// Set your localized subjects for each email template here. | |
$localized_subjects = array( | |
// French | |
'fr_FR' => array( | |
'checkout_check' => "Votre sujet d'email personnalisé ici", | |
'checkout_express' => "Votre sujet d'email personnalisé ici", | |
'checkout_free' => "Votre sujet d'email personnalisé ici", | |
'checkout_freetrial' => "Votre sujet d'email personnalisé ici", | |
'checkout_paid' => "Votre sujet d'email personnalisé ici", | |
'checkout_trial' => "Votre sujet d'email personnalisé ici", | |
), | |
// German | |
'de_DE' => array( | |
'checkout_check' => 'Ihr personalisierter E-Mail-Betreff hier', | |
'checkout_express' => 'Ihr personalisierter E-Mail-Betreff hier', | |
'checkout_free' => 'Ihr personalisierter E-Mail-Betreff hier', | |
'checkout_freetrial' => 'Ihr personalisierter E-Mail-Betreff hier', | |
'checkout_paid' => 'Ihr personalisierter E-Mail-Betreff hier', | |
'checkout_trial' => 'Ihr personalisierter E-Mail-Betreff hier', | |
), | |
); | |
// Get the user's locale and the email template. | |
$user = get_user_by( 'email', $email->email ); | |
$user_locale = get_user_locale( $user ); | |
$template = $email->template; | |
// Let's only do this if the user's locale is not the default "en_US" locale. | |
if ( strpos( 'en_US', $user_locale ) !== false ) { | |
return $subject; | |
} | |
// If the user's locale is in our localized subjects array, and the email template is in the localized subjects array, set the localized subject. | |
if ( array_key_exists( $user_locale, $localized_subjects ) && array_key_exists( $template, $localized_subjects[ $user_locale ] ) ) { | |
$subject = $localized_subjects[ $user_locale ][ $template ]; | |
} | |
return $subject; | |
} | |
add_filter( 'pmpro_email_subject', 'pmpro_email_change_subject_based_on_locale', 20, 2 ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment