Skip Navigation

This thread is resolved. Here is a description of the problem and solution.

Problem:
The client needs to automatically create a duplicate of each Spanish (Spain) page/post for Spanish (Mexico) on their multilingual site, but only for certain pages.
Solution:
We recommend using the WPML hook for duplicating posts automatically. First, ensure you back up your site and database. You can implement the following code in your theme's functions.php file or a custom plugin:

add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
function my_duplicate_on_publish( $post_id ) {
    global $post;
    if (is_null($post) || (defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE) || (isset( $post->post_type ) && $post->post_type == 'revision')) {
        return $post_id;
    }
    remove_action( 'wp_insert_post', 'my_duplicate_on_publish' );
    $is_translated_in_es_mx = apply_filters( 'wpml_element_has_translation', '', $post_id, 'es-mx' );
    if ( !$is_translated_in_es_mx ) {
        do_action( 'wpml_admin_make_post_duplicates', $post_id );
        do_action( 'wpml_set_element_language_details', [
            'element_id' => $post_id,
            'element_type' => 'post_' . $post->post_type,
            'trid' => wpml_get_content_trid( 'post_' . $post->post_type, $post_id ),            
            'language_code' => 'es-mx',            
            'source_language_code' => 'es'
        ] );
    }
    add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
    return $post_id;
}

This script checks if a post is a new or updated Spanish post and duplicates it for Spanish (Mexico) if no translation exists. Modify the condition inside the function if you want to limit this behavior to specific post types.
For more details on the hook used, visit WPML admin make post duplicates.

If this solution does not apply to your case, or if it seems outdated, please check the related known issues, verify the version of the permanent fix, and confirm that you have installed the latest versions of themes and plugins. If further assistance is needed, we encourage you to open a new support ticket.

This is the technical support forum for WPML - the multilingual WordPress plugin.

Everyone can read, but only WPML clients can post here. WPML team is replying on the forum 6 days per week, 22 hours per day.

This topic contains 0 replies, has 1 voice.

Last updated by Andreas W. 1 month, 1 week ago.

Assisted by: Andreas W..

Author Posts
November 11, 2024 at 11:17 am #16388108

alfredoR

Background of the issue:
For this project, I am implementing multilanguage support in several languages like EN voipstudio.com/, ES voipstudio.com/es, PT voipstudio.com/pt. Additionally, I want some localizations, like for example for Mexico (/es-mx). In this case, whenever a user navigates the es-mx site, I want to serve them the /es content, except for some pages where we are localizing the content with some texts specific for Mexico.

Symptoms:
I want to know if there is a way to tell WPML to automatically create an /es-mx page/post for each /es page/post that is a duplicate, except for some pages.

Questions:
Is there a way to tell WPML to automatically create an /es-mx page/post for each /es page/post that is a duplicate, except for some pages?

November 11, 2024 at 8:20 pm #16390131

Andreas W.
Supporter

Languages: English (English ) Spanish (Español ) German (Deutsch )

Timezone: America/Lima (GMT-05:00)

Hello,

As admin you can use this hook to create a es_MX post as duplicate anytime a es_ES post is created or saved.

https://wpml.org/wpml-hook/wpml_admin_make_post_duplicates/

Example:

IMPORTANT:
I did not test this code, please take a backup of your site and database first.

Suppose your Spanish language uses the language code "es" and the custom language uses "es-mx", then this example should work:

add_action( 'wp_insert_post', 'my_duplicate_on_publish' );
function my_duplicate_on_publish( $post_id ) {
    global $post;

    // Don't save for autosave or null post
    if (is_null($post)) {
        return $post_id;
    }

    if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
        return $post_id;
    }

    // Don't save for revisions
    if ( isset( $post->post_type ) && $post->post_type == 'revision' ) {
        return $post_id;
    }

    // Remove this action to avoid recursion
    remove_action( 'wp_insert_post', 'my_duplicate_on_publish' );

    // Check if the post already has a translation in 'es-mx'
    $is_translated_in_es_mx = apply_filters( 'wpml_element_has_translation', '', $post_id, 'es-mx' );

    if ( !$is_translated_in_es_mx ) {
        // Create duplicate in 'es-mx'
        do_action( 'wpml_admin_make_post_duplicates', $post_id );
        
        // Set the language for the duplicate
        do_action( 'wpml_set_element_language_details', [
            'element_id' => $post_id,
            'element_type' => 'post_' . $post->post_type,
            'trid' => wpml_get_content_trid( 'post_' . $post->post_type, $post_id ),
            'language_code' => 'es-mx',
            'source_language_code' => 'es'
        ] );
    }

    // Re-hook this action
    add_action( 'wp_insert_post', 'my_duplicate_on_publish' );

    return $post_id;
}

Now create or update a post, page or custom post in Spanish and you should see it gets duplicated to Spanish (Mexico).

If you would like to have such feature only for posts, you would need to include a conditional into the logic, like:

if ( $post->post_type !== 'post' && $post->post_type !== 'page' ) {
    return $post_id;
}

Best regards
Andreas