apply_filters( 'wpml_copy_post_to_language', int $post_id, string $target_language, bool $mark_as_duplicate )
- ประเภท
- filter
- หมวดหมู่
- การแทรกเนื้อหา
- เวอร์ชันของ WPML
- 4.0.0
คำอธิบาย
ฟิลเตอร์ฮุกนี้ช่วยให้นักพัฒนาสามารถคัดลอกโพสต์ไปยังภาษาที่ระบุด้วย WPML โดยจะทำสำเนาโพสต์ไปยังภาษาปลายทาง และมีตัวเลือกให้ทำเครื่องหมายว่าเป็นสำเนาหรือเป็นการแปล
อาร์กิวเมนต์
ตัวอย่างการใช้งาน
ในตัวอย่างด้านล่าง เราใช้ฟิลเตอร์เพื่อคัดลอกโพสต์ที่มี ID เป็น 1 ไปเป็นภาษาฝรั่งเศส และทำเครื่องหมายว่าเป็นสำเนาของโพสต์ต้นฉบับ
ตัวอย่าง
$duplicate_post_id = apply_filters( 'wpml_copy_post_to_language', 1, 'fr', true );
ในตัวอย่างขั้นสูงที่คุณเห็นด้านล่าง เราจะสาธิตวิธีทำสำเนาโพสต์ที่กำหนดไปยังภาษาอื่น ๆ ที่ใช้งานอยู่ทั้งหมดบนไซต์ WPML ของคุณโดยอัตโนมัติ ยกเว้นภาษาต้นฉบับของโพสต์
ตัวอย่างขั้นสูง
function myplugin_duplicate_copy_post_to_secondary_languages( $post_id, $mark_as_duplicate = false ) {
// Retrieve the post's element type, trid, and existing translations
$element_type = apply_filters( 'wpml_element_type', get_post_type( $post_id ) );
$trid = apply_filters( 'wpml_element_trid', null, $post_id, $element_type );
$translations = (array) apply_filters( 'wpml_get_element_translations', [], $trid, $element_type );
if ( ! $translations ) {
// Handle error or invalid values accordingly
return;
}
// Retrieve the active languages
$wpml_languages = (array) apply_filters('wpml_active_languages', [], 'orderby=id&order=desc');
foreach ( $wpml_languages as $language ) {
// Duplicate the post if no translation exists for the given language
if ( ! isset( $translations[ $language['language_code'] ] ) ) {
apply_filters(
'wpml_copy_post_to_language',
$post_id,
$language['language_code'],
$mark_as_duplicate
);
}
}
}
ฮุกที่เกี่ยวข้อง
wpml_set_element_language_details, wpml_make_post_duplicates, wpml_admin_make_post_duplicates, wpml_delete_package, wpml_import_get_trid_from_imported_translations, wpml_register_string, wpml_register_single_string, wpml_show_package_language_ui, wpml_delete_package_action