1

WPML provides an option to set each domain as a separate language. As a result, we have a German AT (for the .at) domain and a German DE (for the .de domain), for example. English is the default language and it needs to remain that way because we have many other domains for other languages and each of them includes an English (and WPML falls back to the default language if a language doesn't have a translation, which works best as English).

WPML does not provide an option to set a different Fallback language per secondary language. They do provide a way to manually duplicate from one secondary language to another, but that is time consuming each time we want to update something.

I therefore wrote the following code:

function duplicate_wpml_language_on_update( $post_id, $post ) {
    global $sitepress;
    if ( defined( 'ICL_LANGUAGE_CODE' ) && $sitepress->is_translated_post_type( $post->post_type ) ) {
        switch (ICL_LANGUAGE_CODE) {
            case 'fr':
                wp_schedule_single_event( time(), 'duplicate_post', array( $post_id, $post, 'frbe' ) );
                break;
            case 'de':
                wp_schedule_single_event( time(), 'duplicate_post', array( $post_id, $post, 'deat' ) );
        }
    }
}
add_action( 'save_post', 'duplicate_wpml_language_on_update', 10, 2 );
add_action( 'woocommerce_update_product', 'duplicate_wpml_language_on_update', 10, 2 );
add_action( 'wpml_pro_translation_completed', 'duplicate_wpml_language_on_update', 10, 2 );
add_action( 'icl_pro_translation_completed', 'duplicate_wpml_language_on_update', 10, 2 );
add_action( 'wpml_save_translation_data', 'duplicate_wpml_language_on_update', 10, 2 );

function duplicate_post( $post_id, $post, $lang ) {
    global $sitepress;
    $trid = $sitepress->get_element_trid( $post_id, 'post_' . $post->post_type );
    $translations = $sitepress->get_element_translations( $trid, 'post_' . $post->post_type );

    if ( isset( $translations[$lang] ) ) {
        $target_post_id = $translations[$lang]->element_id;
        $duplicate_of = get_post_meta( $target_post_id, '_icl_lang_duplicate_of', true );
        if ( $duplicate_of ) {
            // Duplicate to $lang
            $target_post_id = $sitepress->make_duplicate( $post_id, $lang );
        }
    } 
}
add_action( 'duplicate_post', 'duplicate_post', 10, 3 );

The code works when using the default wordpress editor to edit a post and duplicates it to the secondary language. When using the classic translation editor though, it doesn't work. I'm assuming that the hook is not firing. As you can see, I tried multiple hooks and it's still not working. If it is firing, I don't see any reason for it not to work. I believe the trid would be the same for all languages of a particular post, and I believe _icl_lang_duplicate_of should function the same. Could anything be incorrect about the code? If not, is there another way to do this or get it to fire when saving a post after using the WPML Classic Editor?

0 Answers0