Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Custom script to import CSV file containing terms into a WordPress site using Polylang as multilingual plugin.
// Set the path to the CSV file
$file_path = 'brands.csv';
// Open the CSV file
if (($handle = fopen($file_path, 'r')) !== FALSE) {
// Loop through each row in the CSV file
while (($data = fgetcsv($handle, NULL, ';')) !== FALSE) {
// Create the term object
$term = wp_insert_term(
pll_set_term_language($term['term_id'], 'fr');
if (! is_wp_error($term)) {
add_term_meta($term['term_id'], 'wpc_brand_id', $data[0], true);
add_term_meta($term['term_id'], 'wpc_brand_desc', $data[4], true);
$term_en = wp_insert_term(
['slug' => $data[1] . '-en']
pll_set_term_language($term_en['term_id'], 'en');
if (!is_wp_error($term_en)) {
add_term_meta($term_en['term_id'], 'wpc_brand_id', $data[0], true);
add_term_meta($term_en['term_id'], 'wpc_brand_desc', $data[5], true);
$array = [
'fr' => $term['term_id'],
'en' => $term_en['term_id']
pll_save_term_translations( $array );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment