wpml_switch_language_for_email
- Type
- action
- WPML Version
- 3.2.0
Description
This action hook switches the language of your emails to the user’s preferred language. This is helpful when you need to send emails in different languages. Make sure to reset the language back by using wpml_restore_language_from_email. Also, see the extended documentation: Sending Emails with WPML.
If the given email is not registered, nothing will happen.
These switches nest. Since WPML 5.0, every call to this action opens a language scope, and every call to wpml_restore_language_from_email closes the last scope that is still open. Call the restore once for every switch. Put it in a finally block, so it also runs when your code throws an exception. If you leave a scope open, the rest of the request runs in the recipient’s language.
Arguments
$email (string) The user email.
Example usage
Example
Send an email to all users in their language.
$all_users = get_users();
foreach( $all_users as $user ) {
// Switch language to user language.
do_action('wpml_switch_language_for_email', $user->user_email );
$subject = __('Hello, World!', 'text-domain');
$message = __('This is our newsletter', 'text-domain');
wp_mail( $user->user_email, $subject, $message);
// Switch language back.
do_action('wpml_restore_language_from_email');
}
Related hooks
wpml_post_language_details, wpml_restore_language_from_email, wpml_switch_language, wpml_element_language_code, wpml_element_language_details