Skip to content

Commit 6a737a5

Browse files
committed
v 0.118.0
Sample : handle multilingual with polylang
1 parent 6b29e85 commit 6a737a5

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

tools/sample.php

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,15 @@
139139
continue;
140140
}
141141

142+
$is_multilingual = function_exists('pll_get_post_translations');
143+
$languages = array();
144+
if ($is_multilingual) {
145+
$languages_raw = get_terms('term_language', ['hide_empty' => false]);
146+
foreach ($languages_raw as $lang) {
147+
$languages[str_replace('pll_', '', $lang->slug)] = $lang->name;
148+
}
149+
}
150+
142151
/* Create post */
143152
$random_images = get_posts(array(
144153
'post_type' => 'attachment',
@@ -156,18 +165,41 @@
156165
/* Add images */
157166
$post_title = $raw_titles[mt_rand(0, $nb_raw_titles - 1)] . ' ' . $label . ' #' . $i;
158167

159-
$post_id = wp_insert_post(array(
168+
$post_infos = array(
160169
'post_title' => $post_title,
161170
'post_content' => $raw_contents[mt_rand(0, $nb_raw_contents - 1)],
162171
'post_type' => $pt,
163172
'post_status' => 'publish',
164173
'post_author' => 1
165-
));
174+
);
175+
176+
$post_id = wp_insert_post($post_infos);
166177
$_hasImport = true;
167178

168179
/* Taxonomies */
180+
$thumbnail_id = false;
169181
if ($post_id && !empty($random_images)) {
170-
set_post_thumbnail($post_id, $random_images[array_rand($random_images)]);
182+
$thumbnail_id = $random_images[array_rand($random_images)];
183+
set_post_thumbnail($post_id, $thumbnail_id);
184+
}
185+
186+
if ($is_multilingual && $languages) {
187+
$first_code = array_keys($languages)[0];
188+
$translations = array($post_id);
189+
foreach ($languages as $lang_code => $lang_name) {
190+
if ($lang_code === $first_code) {
191+
continue;
192+
}
193+
$post_infos['post_title'] = $post_title . ' (' . $lang_name . ')';
194+
$translated_post_id = wp_insert_post($post_infos);
195+
if ($translated_post_id) {
196+
pll_set_post_language($translated_post_id, $lang_code);
197+
pll_save_post_translations(array_merge(pll_get_post_translations($post_id), [$lang_code => $translated_post_id]));
198+
}
199+
if($thumbnail_id){
200+
set_post_thumbnail($translated_post_id, $thumbnail_id);
201+
}
202+
}
171203
}
172204

173205
foreach ($taxonomies as $tax_name) {

wputools.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
WPUTools(){
44

5-
local _WPUTOOLS_VERSION='0.117.4';
5+
local _WPUTOOLS_VERSION='0.118.0';
66
local _PHP_VERSIONS=(7.0 7.1 7.2 7.3 7.4 8.0 8.1 8.2 8.3 8.4 8.5 9.0)
77
local _PHP_VERSIONS_OBSOLETES=(7.0 7.1 7.2 7.3 7.4 8.0)
88
local _PHP_VERSIONS_ADVANCED=(8.3 8.4 8.5 9.0)

0 commit comments

Comments
 (0)