Last updated on October 18, 2023

WPML translate MailPoet newsletters

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Translate MailPoet newsletters with WPML.

To translate MailPoet newsletters using WPML, you can use the wpml_translate function provided by the WPML plugin. This function allows you to translate strings in your WordPress site, including the content of your MailPoet newsletters.

Here’s an example code snippet that demonstrates how to use the wpml_translate function to translate a MailPoet newsletter:

function wpsnippets_translate_mailpoet_newsletter( $newsletter_content ) {
    if ( function_exists( 'wpml_translate' ) ) {
        $translated_content = wpml_translate( 'mailpoet', 'newsletter', $newsletter_content );
        return $translated_content;
    } else {
        return $newsletter_content;
    }
}

In this example, the wpsnippets_translate_mailpoet_newsletter function takes the $newsletter_content as a parameter. It checks if the wpml_translate function exists (to ensure that WPML is active), and if it does, it uses the wpml_translate function to translate the $newsletter_content using the ‘mailpoet’ domain and ‘newsletter’ context. The translated content is then returned.

You can use this code snippet by calling the wpsnippets_translate_mailpoet_newsletter function and passing the MailPoet newsletter content as an argument. The function will return the translated content if WPML is active, or the original content if WPML is not installed or activated.

This code snippet can be useful if you have a multilingual WordPress site using WPML and want to provide translated versions of your MailPoet newsletters to your subscribers.

Examples

Example 1: Translating MailPoet newsletters using WPML

This use case demonstrates how to translate MailPoet newsletters using WPML. The code example shows how to retrieve the newsletter content in the current language and display it on the frontend.

<?php
function wpsnippets_translate_mailpoet_newsletter( $newsletter_id ) {
    if ( function_exists( 'icl_object_id' ) ) {
        $translated_id = icl_object_id( $newsletter_id, 'newsletter', true );
        if ( $translated_id ) {
            $newsletter = MailPoetNewsletter::get( $translated_id );
            if ( $newsletter ) {
                echo $newsletter->content;
            }
        }
    }
}

Explanation: The wpsnippets_translate_mailpoet_newsletter function checks if the WPML plugin is active and retrieves the translated newsletter ID using the icl_object_id function. If a translation exists, it retrieves the newsletter content using the MailPoet API and displays it on the frontend.

Example 2: Translating MailPoet newsletters with language switcher

This use case demonstrates how to translate MailPoet newsletters and provide a language switcher for users to switch between different language versions of the newsletter. The code example shows how to generate a language switcher and display the translated newsletter content based on the selected language.

<?php
function wpsnippets_translate_mailpoet_newsletter_with_switcher( $newsletter_id ) {
    if ( function_exists( 'icl_get_languages' ) ) {
        $languages = icl_get_languages();
        foreach ( $languages as $language ) {
            $translated_id = icl_object_id( $newsletter_id, 'newsletter', true, $language['language_code'] );
            if ( $translated_id ) {
                $newsletter = MailPoetNewsletter::get( $translated_id );
                if ( $newsletter ) {
                    echo '<h2>' . $language['native_name'] . '</h2>';
                    echo $newsletter->content;
                }
            }
        }
    }
}

Explanation: The wpsnippets_translate_mailpoet_newsletter_with_switcher function uses the icl_get_languages function to retrieve the available languages. It then loops through each language and retrieves the translated newsletter ID using the icl_object_id function. If a translation exists, it retrieves the newsletter content and displays it along with the language name on the frontend.

Example 3: Translating MailPoet newsletters programmatically

This use case demonstrates how to programmatically translate MailPoet newsletters using WPML. The code example shows how to create a translated version of a newsletter and update its content in a specific language.

<?php
function wpsnippets_translate_mailpoet_newsletter_programmatically( $newsletter_id, $language_code ) {
    if ( function_exists( 'icl_object_id' ) ) {
        $translated_id = icl_object_id( $newsletter_id, 'newsletter', false, $language_code );
        if ( ! $translated_id ) {
            $translated_id = icl_copy_post( $newsletter_id, $language_code );
        }
        if ( $translated_id ) {
            $newsletter = MailPoetNewsletter::get( $translated_id );
            if ( $newsletter ) {
                $newsletter->content = 'Translated content goes here';
                $newsletter->save();
            }
        }
    }
}

Explanation: The wpsnippets_translate_mailpoet_newsletter_programmatically function checks if the WPML plugin is active and retrieves the translated newsletter ID using the icl_object_id function. If a translation doesn’t exist, it creates a new translated version using the icl_copy_post function. It then retrieves the newsletter object and updates its content with the translated content. Finally, it saves the newsletter to apply the changes.

Last updated on October 18, 2023. Originally posted on November 27, 2023.

Leave a Reply