Skip Navigation

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.

Sun Mon Tue Wed Thu Fri Sat
- - 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00 9:00 – 18:00
- - - - - - -

Supporter timezone: America/Lima (GMT-05:00)

This topic contains 0 replies, has 1 voice.

Last updated by Andreas W. 3 days, 11 hours 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 ) 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