Created
March 4, 2022 20:07
-
-
Save andrii-kvlnko/ffcb685576ec043f429f74af2a2c1ea6 to your computer and use it in GitHub Desktop.
Create terms translations WPML
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_action( 'wp', function () { | |
if ( ! isset( $_GET['test-add-product-categories-fr'] ) ) { | |
return; | |
} | |
$categories = [ | |
'Climatiseur' => [ | |
'args' => [ | |
'parent' => 0, | |
], | |
'en' => 411, | |
], | |
'Cave' => [ | |
'args' => [ | |
'parent' => 0, | |
], | |
'en' => 414, | |
], | |
'Fours' => [ | |
'args' => [ | |
'parent' => 0, | |
], | |
'en' => 412, | |
], | |
'Géothermie' => [ | |
'args' => [ | |
'parent' => 0, | |
], | |
'en' => 413, | |
], | |
'Pompe à chaleur' => [ | |
'args' => [ | |
'parent' => 0, | |
], | |
'en' => 410, | |
], | |
'Déshumidification piscine' => [ | |
'args' => [ | |
'parent' => 0, | |
], | |
'en' => 416, | |
], | |
'Plancher radiant' => [ | |
'args' => [ | |
'parent' => 0, | |
], | |
'en' => 415, | |
], | |
"Échangeurs d'air" => [ | |
'args' => [ | |
'parent' => 0, | |
], | |
'en' => 143, | |
], | |
'Humidificateurs' => [ | |
'args' => [ | |
'parent' => 0, | |
], | |
'en' => 144, | |
], | |
]; | |
$categories = [ | |
'Central' => [ | |
'args' => [ | |
'parent' => 426, | |
], | |
'en' => 419, | |
], | |
'Mural' => [ | |
'args' => [ | |
'parent' => 426, | |
], | |
'en' => 418, | |
], | |
'Électrique' => [ | |
'args' => [ | |
'parent' => 428, | |
], | |
'en' => 417, | |
], | |
'Gaz' => [ | |
'args' => [ | |
'parent' => 428, | |
], | |
'en' => 420, | |
], | |
]; | |
$language = 'fr'; | |
$switcher = new \Plugins\Thermolux\Services\Multilingual\WPML_Language_Switcher(); | |
$switcher->switch_to( $language ); | |
global $sitepress; | |
$taxonomy = 'product_cat'; | |
foreach ( $categories as $name => $settings ) { | |
$term_data = wp_insert_term( $name, $taxonomy, $settings['args'] ); | |
$fr_term_id = $term_data['term_id']; | |
$sitepress_taxonomy = sprintf( 'tax_%s', $taxonomy ); | |
$en_term_id = $settings['en']; | |
$translation_id = $sitepress->get_element_trid( $en_term_id, $sitepress_taxonomy ); | |
$sitepress->set_element_language_details( | |
$fr_term_id, | |
$sitepress_taxonomy, | |
$translation_id, | |
$language, | |
$sitepress->get_default_language() | |
); | |
} | |
exit; | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment