WPML
do_action( 'wpml_set_element_language_details', array $args )
タイプ
action
カテゴリー
コンテンツの挿入
WPMLバージョン
3.2.7

説明

アクションフックを使用すると、要素の言語情報を設定または更新できます。基本的な用途とは別に、このフックは一方の要素をもう一方の要素の「翻訳」として、2つの要素を関連付ける必要がある場合に役立ちます。この例については、以下の「フックの使用例」セクションを参照してください。

引数

$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) 要素が割り当てられるtridFALSEに設定すると、要素に新しいtridが作成され、その要素との間の潜在的な翻訳関係がすべて失われます。
  • language_code(string) 要素の言語コード。
  • source_language_code(string) 要素の翻訳元言語コード。NULLは元の要素(他の要素の翻訳ではない要素)のために予約されています。設定されていない場合、デフォルトはNULLになります。
  • check_duplicates(bool) デフォルトはTRUE.です。これは、同じIDを持つ異なるタイプの別の要素がすでに存在するかどうかを確認し、「Element ID already exists with a different type」というメッセージを含むユーザーレベルの通知(E_USER_NOTICE)を出力します。

使用例

以下の例では、my_insert_posts()関数を使用してフロントエンドから2件の投稿を挿入します。1つは元の投稿で、もう1つは翻訳です。 この関数はelement_connect_on_insert()内で呼び出されます。これは2件の投稿を互いに接続するメイン関数です。内部で他の2つの便利なWPMLフックを使用していることに注意してください。要素タイプをWPMLが理解できる値にフィルタリングするためのwpml_element_typeフィルターと、元のtridと言語コードを取得するためのwpml_element_language_detailsフィルターです。これらはすべて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件) →