WPML
do_action( 'wpml_set_element_language_details', array $args )
ประเภท
action
เวอร์ชันของ WPML
3.2.7

คำอธิบาย

แอ็กชันฮุกช่วยให้สามารถตั้งค่าหรืออัปเดตข้อมูลภาษาของอิลิเมนต์ได้ นอกเหนือจากการใช้งานตามปกติแล้ว ฮุกนี้ยังมีประโยชน์เมื่อคุณต้องการเชื่อมต่ออิลิเมนต์สองรายการ โดยกำหนดให้รายการหนึ่งเป็น “การแปลของ” อีกรายการหนึ่ง โปรดดูตัวอย่างในส่วน “ตัวอย่างการใช้งานฮุก” ด้านล่าง

อาร์กิวเมนต์

$args
(array) (จำเป็น) อาร์เรย์ของอาร์กิวเมนต์ที่จะใช้
  • element_id(bool) ใช้ term_taxonomy_id สำหรับอนุกรมวิธาน และ post_id สำหรับโพสต์
  • element_type(string) ประเภทขององค์ประกอบ สามารถเป็นประเภทโพสต์: post_post, post_page, post_attachment, post_nav_menu_item, post_{custom post key} หรืออนุกรมวิธาน: tax_category, tax_post_tag, tax_nav_menu, tax_{custom taxonomy key} ค่าเริ่มต้นคือ post_post หากไม่ได้ตั้งค่าไว้
  • trid(int) trid ที่จะกำหนดให้กับองค์ประกอบ หากตั้งค่าเป็น FALSE จะเป็นการสร้าง trid ใหม่สำหรับองค์ประกอบ ซึ่งทำให้ความสัมพันธ์ของการแปลที่อาจมีอยู่หายไป
  • language_code(string) รหัสภาษาสำหรับองค์ประกอบ
  • source_language_code(string) รหัสภาษาต้นทางสำหรับองค์ประกอบ NULL สงวนไว้สำหรับองค์ประกอบต้นฉบับ เช่น องค์ประกอบที่ไม่ได้เป็นคำแปลขององค์ประกอบอื่น ค่าเริ่มต้นคือ NULL หากไม่ได้ตั้งค่าไว้
  • check_duplicates(bool) ค่าเริ่มต้นคือ TRUE. ฟังก์ชันนี้จะตรวจสอบว่ามีองค์ประกอบอื่นที่เป็นคนละประเภทแต่มี ID เดียวกันอยู่แล้วหรือไม่ และจะแสดงประกาศระดับผู้ใช้ (E_USER_NOTICE) พร้อมข้อความ “Element ID already exists with a different type”

ตัวอย่างการใช้งาน

ในตัวอย่างด้านล่าง เราใช้ฟังก์ชัน my_insert_posts() เพื่อแทรกโพสต์สองรายการจากส่วนหน้าเว็บไซต์ โดยเป็นต้นฉบับหนึ่งรายการและคำแปลหนึ่งรายการ ฟังก์ชันนี้ถูกเรียกใช้ใน element_connect_on_insert() ซึ่งเป็นฟังก์ชันหลักของเราที่จะเชื่อมโยงโพสต์ทั้งสองเข้าด้วยกัน สังเกตการใช้ฮุกของ WPML ที่มีประโยชน์อีกสองตัวภายใน ได้แก่ ฟิลเตอร์ wpml_element_type สำหรับกรองประเภทองค์ประกอบให้เป็นค่าที่ WPML เข้าใจ และ ฟิลเตอร์ wpml_element_language_details สำหรับดึงข้อมูล trid ต้นฉบับและรหัสภาษา ทั้งหมดนี้จะฮุกเข้ากับ wp_footer เพื่อให้ฟิลเตอร์และการดำเนินการของเราทำงานเมื่อมีการโหลดส่วนท้ายบนส่วนหน้าเว็บไซต์ ตัวอย่าง
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 );
	}
}

wpml_make_post_duplicates, wpml_admin_make_post_duplicates, wpml_copy_post_to_language, 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

ฮุก การแทรกเนื้อหา ทั้งหมด 10 รายการ →