Translatepress email notification compatibility

From Lonsdale201/wp-agent-skills · View interactive version

Translatepress email notification compatibility is an AI agent skill from the GitHub repository Lonsdale201/wp-agent-skills. Audit or implement TranslatePress-compatible WordPress plugin emails, WooCommerce emails, transactional notifications, cron/CLI emails, wp_mail usage, preferred-user-language delivery, trp_translate(), trp_switch_language(), trp_language user/order meta, and email gettext scanning. Use when code sen

How to use this skill

Skill file

TranslatePress Email And Notification Compatibility

TranslatePress can translate wp_mail() subject/body, store a user's preferred language, and translate WooCommerce emails in the customer/admin language. Plugin code must render emails in a stable language context and avoid one-email-many-languages shortcuts.

When to use this skill

Trigger when ANY of the following is true:

Baseline checks

Verify the installed stack:

wp plugin list --fields=name,status,version | grep -E 'translatepress|woocommerce'
wp eval 'echo defined( "TRP_PLUGIN_VERSION" ) ? TRP_PLUGIN_VERSION : "missing";'
wp option get trp_settings --format=json

TranslatePress Multilingual 3.2.1 hooks wp_mail at priority 1 through TRP_Translation_Render::wp_mail_filter(). WooCommerce email language support is initialized on init and requires WC_VERSION >= 6.8.0.

Prefer wp_mail for normal emails

If the plugin can use wp_mail(), do that. TranslatePress filters subject and message, runs whitelisted conditional language shortcodes, and switches to the first recipient's preferred language when that recipient is a registered user.

Good pattern:

$subject = __( 'Your certificate is ready', 'myplugin' );
$message = myplugin_render_email_template( $certificate_id );

wp_mail( $user->user_email, $subject, $message, array( 'Content-Type: text/html; charset=UTF-8' ) );

Do not pre-render a translated body in one global language and then send it to multiple recipients. TranslatePress chooses one language per wp_mail() call, using the first recipient when multiple recipients are provided. Send separate emails when recipients may have different languages.

Preferred user language

TranslatePress stores the front-end language preference in user meta:

The profile UI comes from TRP_Preferred_User_Language. Logged-in front-end visits update trp_language unless "Always use this language" is enabled.

When rendering custom email templates before wp_mail(), switch language during template rendering and restore it immediately:

$language = get_user_meta( $user_id, 'trp_language', true );

if ( function_exists( 'trp_switch_language' ) && ! empty( $language ) ) {
    trp_switch_language( $language );

    try {
        $subject = __( 'Your renewal is due', 'myplugin' );
        $message = myplugin_render_email_template( $subscription_id );
    } finally {
        trp_restore_language();
    }
} else {
    $subject = __( 'Your renewal is due', 'myplugin' );
    $message = myplugin_render_email_template( $subscription_id );
}

Use this for custom transports that bypass wp_mail() or for templates that must be rendered before calling wp_mail(). Do not leave TRP_LANGUAGE, locale, or plugin_locale switched after rendering.

Use trp_translate for isolated strings

Use trp_translate() when you need to translate a specific stored string or HTML fragment:

if ( function_exists( 'trp_translate' ) ) {
    $message = trp_translate( $message, $language, true );
}

The third parameter defaults to true, which wraps output with data-no-translation to prevent over-translation if the result later appears in rendered HTML. Set it to false only when the translated string will not be detected again by TranslatePress.

Do not pass volatile personalized fragments as one whole translation unit if they change for every user. Split stable prose from variables.

Conditional language shortcodes in email templates

For admin-editable email bodies, allow shortcodes so site owners can use current conditional language shortcodes:

[language-include lang="en_US"]English content[/language-include]
[language-exclude lang="de_DE"]Not German content[/language-exclude]

TranslatePress core whitelists trp_language, language-include, and language-exclude in wp_mail_filter(). Keep [trp_language] only for legacy content; core marks it deprecated.

If the plugin manually runs shortcodes before wp_mail(), restrict the shortcode surface. Do not run arbitrary shortcodes in untrusted email content.

Gettext and email template paths

Use normal WordPress i18n in PHP email templates:

printf(
    '<p>%s</p>',
    esc_html__( 'Thanks for learning with us.', 'myplugin' )
);

TranslatePress gettext scanning marks strings as email text when file paths contain:

If the plugin stores email templates in another path, extend the source-confirmed filter:

add_filter( 'trp_email_paths_', static function ( array $paths ): array {
    $paths[] = 'resources/mail/';
    return $paths;
} );

This helps the String Translation email filter find those gettext strings. It does not replace normal .pot extraction or text-domain correctness.

WooCommerce emails

TranslatePress core handles WooCommerce emails when WooCommerce is available:

For WooCommerce extensions:

Custom language correction:

add_filter( 'trp_woo_email_language', static function ( string $language, bool $is_customer_email, array $recipients, int $user_id ): string {
    if ( $is_customer_email || $user_id <= 0 ) {
        return $language;
    }

    $preferred = get_user_meta( $user_id, 'trp_language', true );
    return $preferred ?: $language;
}, 10, 4 );

Async, cron, and CLI

Cron and webhook emails often run without a front-end request language. Explicitly choose a target language from durable data:

Do not use the current request URL, cookie, or admin user's current language as the recipient language for delayed jobs.

Test matrix

Test these paths before release:

Critical rules

Cross-references

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.