When using Polylang to build multilingual websites, the two language versions of an article differ only in language, while everything else is basically the same. If we enter or configure all of those identical parts again, the process becomes very inefficient, especially when an article contains a large number of custom fields.
When a large number of custom fields also need to be published in both language versions, we install a post duplication plugin for clients so they do not have to enter the same custom fields twice. However, that convenience does not automatically create the Polylang association between the two language versions. While editing the post, you still need to connect them manually in Polylang. That manual step is rather tedious, so we wanted to find a way to reduce the hassle as much as possible.
Automatically Establish Polylang Language Associations When Duplicating Posts
We use the Post Duplicator plugin to duplicate posts. After reviewing the plugin code, we found the mtphr_post_duplicator_created hook. That hook allows us to perform more actions on the duplicated post after the duplication is complete. Combined with some Polylang functions, we can use the following code to automatically establish the association between the two language versions after duplication.
In the code below, zh is the alias for Simplified Chinese in Polylang, and de is the alias for German. After the user clicks to duplicate the post, the code gets the current language of the original post, then determines and sets the language of the duplicated post. Finally, it sets the relationship between the two language versions.
/**
* 创建文章时创建文章关系
*/
add_action('mtphr_post_duplicator_created', function ($original_id, $duplicate_id)
{
$original_id = absint($original_id);
$original_lang = pll_get_post_language($original_id, 'slug');
$new_lang = ($original_lang == 'de') ? 'zh' : 'de';
pll_set_post_language($duplicate_id, $new_lang);
pll_save_post_translations([
$original_lang => $original_id,
$new_lang => $duplicate_id,
]);
}, 10, 2);After Duplicating the Post, Set the Corresponding Taxonomy Terms for the New Language
With the code above, we duplicate the post content and custom fields. However, the post’s custom taxonomies are not copied over. This happens because the taxonomy terms from the source language do not exist in the new language context. What exists there are only the translated taxonomy terms for the target language.
Just like posts, taxonomy terms in the source language and the new language can also be associated with each other. We can use the Polylang function pll_get_term() to get the taxonomy term that corresponds to the new language, then associate it with the duplicated post. In this way, the taxonomy terms are duplicated as well. The specific code is as follows.
/**
* 复制后,设置新语言的分类方法
*
* @param $original_id 原始文章 ID
* @param $duplicate_id 复制后的文章 ID
* @param $taxonomy 分类法名称
* @param $new_lang 新语言别名
*/
function polylang_set_new_terms($original_id, $duplicate_id, $taxonomy, $new_lang)
{
$terms = wp_get_post_terms($original_id, $taxonomy);
foreach ($terms as $term) {
$new_lang_term_id = pll_get_term($term->term_id, $new_lang);
wp_set_post_terms($duplicate_id, $new_lang_term_id, $taxonomy);
}
}Use the function above at the appropriate place in the first code block, and after duplication you will be able to assign the corresponding taxonomy terms for the post in the new language.
The code above only handles the automatic setup for two languages. If your site supports more than two languages, the code will become much more complex, and you may even need to modify the post duplication plugin itself to make it work. If you have that need, you can use the code above as a reference and try building on it.
