Bài toán: Bạn có 1 website 2 ngôn ngữ vd: Tiếng Việt(vi) và Tiếng anh(en_US), website bạn sử dụng plugin WPML. Bạn muốn sử dụng REST API để copy bài viết này sang 1 website với nội dung tương tự cho cả 2 ngôn ngữ vi và en_US.
Hướng giải quyết:
B1/ Tạo 2 bài viết ở website mới sử dụng REST API
B2/ Hook vô các hàm wpml hỗ trợ để liên kết cái bài viết này theo bản dịch
b3/ Cập nhật lại slug của ngôn ngữ dịch nếu cần thiết (trường hợp cập nhật lại khi “slug tiếng việt” trùng “slugin tiếng anh”)
Cách thực hiện: xem đoạn mã bên dưới
Lưu ý:
– Hàm hook tới plugin wpml này chỉ áp dụng đối với WPML Version: >=3.2.7

add_action('wp_footer', 'element_connect_on_insert');
 
function my_insert_posts() {
    $output = array();
 
    // Create original post object
    $my_original_post = array(
        'post_title'    => 'My original post',
        'post_content'  => 'This is my original post.',
        'post_status'   => 'publish',
        'post_author'   => 1,
        'post_category' => array(1)
    );
 
    // Create translation post object
    $my_translated_post = array(
        'post_title'    => 'My translated post',
        'post_content'  => 'This is my translated post.',
        'post_status'   => 'publish',
        'post_author'   => 1,
        'post_category' => array(2) // NOTE: this is the translated category id!
    );
 
    // Insert the 2 posts into the database
    $original_post_id = wp_insert_post( $my_original_post );
    $translated_post_id = wp_insert_post( $my_translated_post );
 
    return $output = array(
        'original' => $original_post_id,
        'translation' => $translated_post_id
    );
}
 
 
function element_connect_on_insert() {
    $inserted_post_ids = my_insert_posts();
 
    if ( $inserted_post_ids) {
        // https://wpml.org/wpml-hook/wpml_element_type/
        $wpml_element_type = apply_filters( 'wpml_element_type', 'post' );
         
        // get the language info of the original post
        // https://wpml.org/wpml-hook/wpml_element_language_details/
        $get_language_args = array('element_id' => $inserted_post_ids['original'], 'element_type' => 'post' );
        $original_post_language_info = apply_filters( 'wpml_element_language_details', null, $get_language_args );
         
        $set_language_args = array(
            'element_id'    => $inserted_post_ids['translation'],
            'element_type'  => $wpml_element_type,
            'trid'   => $original_post_language_info->trid,
            'language_code'   => 'de',
            'source_language_code' => $original_post_language_info->language_code
        );
 
        do_action( 'wpml_set_element_language_details', $set_language_args );
    }
}

 

Cách 2 sử dụng WPML API

/**
 * Creates a translation of a post (to be used with WPML)
 *  
 * @param int $post_id The ID of the post to be translated.
 * @param string $post_type The post type of the post to be transaled (ie. 'post', 'page', 'custom type', etc.).
 * @param string $lang The language of the translated post (ie 'fr', 'de', etc.).
 *    
 * @return the translated post ID
 *  */
function mwm_wpml_translate_post( $post_id, $post_type, $lang ){

    // Include WPML API
    include_once( WP_PLUGIN_DIR . '/sitepress-multilingual-cms/inc/wpml-api.php' );

    // Define title of translated post
    $post_translated_title = get_post( $post_id )->post_title . ' (' . $lang . ')';

    // Insert translated post
    $post_translated_id = wp_insert_post( array( 'post_title' => $post_translated_title, 'post_type' => $post_type ) );

    // Get trid of original post
    $trid = wpml_get_content_trid( 'post_' . $post_type, $post_id );

    // Get default language
    $default_lang = wpml_get_default_language();

    // Associate original post and translated post
    global $wpdb;
    $wpdb->update( 
        $wpdb->prefix.'icl_translations', 
        array( 
            'trid' => $trid, 
            'language_code' => $lang, 
            'source_language_code' => $default_lang 
        ), 
        array( 
            'element_type' => $post_type, 
            'element_id' => $post_translated_id 
        ) 
    );

    // Return translated post ID
    return $post_translated_id;

}

Bài viết liên quan

post-no-image

Chia sẻ một số thư viện loading css đẹp

giai-phap-chinh-css-khong-bi-luu-cache-trinh-duyet

Giải pháp tốt khi chỉnh sửa css mà không sợ bị lưu cache trình duyệt

post-no-image

Một số css tùy biến thẻ input type: select, number

post-no-image

Tắt lazyload cho một số hình ảnh

post-no-image

Tuỳ biến layout YITH WooCommerce Wishlist

post-no-image

Một số hook hoặc filter thường sử dụng trong plugin WPML