Last updated on October 18, 2023

WPML multilingual blog post

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

Create multilingual blog posts with WPML.

To create a multilingual blog post using WPML (WordPress Multilingual Plugin), you need to follow these steps:

  1. Install and activate the WPML plugin on your WordPress site.
  2. Configure the plugin by selecting the languages you want to use for your blog posts.
  3. Create a new blog post or edit an existing one.
  4. Use the WPML language switcher to switch to the desired language for the blog post.
  5. Translate the content of the blog post into the selected language.
  6. Save or update the blog post.

Here’s an example of how to use the WPML API to programmatically create a multilingual blog post:

function wpsnippets_create_multilingual_blog_post() {
    // Create a new post in the default language
    $post_id = wp_insert_post( array(
        'post_title'   => 'Hello World',
        'post_content' => 'This is the default language content.',
        'post_status'  => 'publish',
        'post_type'    => 'post',
    ) );

    // Get the available languages
    $languages = apply_filters( 'wpml_active_languages', null, 'orderby=id&order=desc' );

    // Loop through the languages and translate the post
    foreach ( $languages as $language ) {
        if ( $language['active'] ) {
            continue; // Skip the default language
        }

        // Translate the post title and content
        $translated_title   = apply_filters( 'wpml_translate_single_string', 'Hello World', 'Post', 'post_title', $language['language_code'] );
        $translated_content = apply_filters( 'wpml_translate_single_string', 'This is the translated content.', 'Post', 'post_content', $language['language_code'] );

        // Create a new post translation
        $translation_id = wp_insert_post( array(
            'post_title'     => $translated_title,
            'post_content'   => $translated_content,
            'post_status'    => 'publish',
            'post_type'      => 'post',
            'post_parent'    => $post_id,
            'menu_order'     => $language['menu_order'],
            'comment_status' => 'closed',
        ) );

        // Set the language for the translation
        do_action( 'wpml_set_element_language_details', $translation_id, 'post_post', $language['language_code'] );
    }
}

This code snippet demonstrates how to programmatically create a multilingual blog post using WPML. It uses the wp_insert_post() function to create the initial post in the default language, and then uses the WPML API functions to translate and create post translations in other languages. The wpml_translate_single_string() function is used to translate the post title and content, and the wpml_set_element_language_details() function is used to set the language for each translation.

Examples

Example 1: Creating a Multilingual Blog Post with WPML

This example demonstrates how to create a multilingual blog post using WPML. The code example shows how to use the wp_insert_post() function to create a new post in multiple languages.

function wpsnippets_create_multilingual_post() {
    // Set the post data
    $post_data = array(
        'post_title' => 'Hello World',
        'post_content' => 'This is a test post.',
        'post_status' => 'publish',
        'post_author' => 1,
        'post_type' => 'post',
    );

    // Create the post in the default language
    $post_id = wp_insert_post( $post_data );

    // Translate the post into other languages
    if ( function_exists( 'wpml_get_languages' ) ) {
        $languages = wpml_get_languages();

        foreach ( $languages as $language ) {
            if ( $language['language_code'] !== ICL_LANGUAGE_CODE ) {
                $translated_post_data = array(
                    'ID' => $post_id,
                    'post_title' => 'Translated Title',
                    'post_content' => 'Translated content.',
                    'icl_language_code' => $language['language_code'],
                );

                wp_update_post( $translated_post_data );
            }
        }
    }
}

This code example demonstrates how to create a multilingual blog post using WPML. It uses the wp_insert_post() function to create a new post in the default language and then uses the wp_update_post() function to translate the post into other languages. The wpml_get_languages() function is used to retrieve the list of available languages.

Example 2: Retrieving Multilingual Blog Posts with WPML

This example demonstrates how to retrieve multilingual blog posts using WPML. The code example shows how to use the WP_Query class to query posts in a specific language.

function wpsnippets_get_multilingual_posts() {
    $args = array(
        'post_type' => 'post',
        'posts_per_page' => 5,
        'lang' => 'en', // Language code
    );

    $query = new WP_Query( $args );

    if ( $query->have_posts() ) {
        while ( $query->have_posts() ) {
            $query->the_post();

            // Output post title and content
            the_title();
            the_content();
        }
    }

    wp_reset_postdata();
}

This code example demonstrates how to retrieve multilingual blog posts using WPML. It uses the WP_Query class to query posts and includes the 'lang' parameter to specify the desired language. The retrieved posts are then looped through using a while loop and the post title and content are outputted using the the_title() and the_content() functions.

Example 3: Switching Language for Multilingual Blog Posts with WPML

This example demonstrates how to switch the language for multilingual blog posts using WPML. The code example shows how to use the icl_object_id() function to retrieve the translated post ID in a specific language.

function wpsnippets_switch_language() {
    $post_id = get_the_ID();
    $translated_post_id = icl_object_id( $post_id, 'post', false, 'fr' ); // Language code

    if ( $translated_post_id ) {
        // Output translated post title and content
        echo get_the_title( $translated_post_id );
        echo get_post_field( 'post_content', $translated_post_id );
    }
}

This code example demonstrates how to switch the language for multilingual blog posts using WPML. It uses the icl_object_id() function to retrieve the translated post ID in a specific language. The retrieved translated post ID is then used to output the translated post title and content using the get_the_title() and get_post_field() functions.

Last updated on October 18, 2023. Originally posted on October 20, 2023.

Leave a Reply

Your email address will not be published. Required fields are marked *