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