benecaster_email_template_locale
Filters the locale used to resolve the email template file path. Fires per email send, after the default locale has been determined from WordPress settings. Use to send specific email types or subscriber segments in a different locale.
Return a locale string such as es_MX. Cast with (string).
An unknown locale produces an empty email, silently. The returned value is used to locate the template, and when no template is found for it the renderer returns [ 'html' => '', 'text' => '' ] — the send still happens, so the subscriber receives a blank message and nothing is logged as an error. Return a locale you have actually shipped templates for.
This is the supported way to set an email’s language: never call get_locale() directly in a renderer, which is what this filter exists to displace.
Parameters
| Name | Type | Default | Description |
|---|---|---|---|
$locale |
string |
— | Locale code (e.g. 'en_US') used for template file resolution |
$user_id |
int|null |
— | WordPress user ID of the recipient, or null for admin emails |
$show_id |
int|null |
— | ID of the show, or null when not show-specific |
$email_type |
string |
— | Email type string identifying which template is being resolved |
Returns:
string
Example
add_filter( 'benecaster_email_template_locale', function( $locale, $user_id, $show_id, $email_type ) {
// Send welcome emails to Spanish speakers in es_ES.
if ( 'welcome' === $email_type && $user_id ) {
$lang = get_user_meta( $user_id, 'locale', true );
if ( str_starts_with( $lang, 'es_' ) ) {
return 'es_ES';
}
}
return $locale;
}, 10, 4 );
Affects
- Email template file resolution
Need this built rather than just documented? See our services →