Skip to content

Instantly share code, notes, and snippets.

@wpchannel
Last active February 28, 2023 07:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wpchannel/75a8b29a13b970f3dc371a7ab7dde93e to your computer and use it in GitHub Desktop.
Save wpchannel/75a8b29a13b970f3dc371a7ab7dde93e to your computer and use it in GitHub Desktop.
Custom script to import CSV file containing terms into a WordPress site using Polylang as multilingual plugin.
<?php
require_once('wp/wp-load.php');
// 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(
$data[1],
'brand'
);
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(
$data[1],
'brand',
['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 );
}
fclose($handle);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment