Back to Top

How to import terms from csv in wordpress?

To import terms from csv in wordpress

WordPress is the most popular CMS and with WordPress building a website is very quick and easy. Today I am going to explain about to import terms like (categories, tags, or terms of custom taxonomy) from CSV into WordPress.

Here, I am going to explain code step by step:

First of all, create one file into root folder where you have stored WordPress or you can create into your theme folder. If you create file into theme, you need to change $base_dir value variable.

$base_dir = dirname(__FILE__));

Next,we will calls csv_to_array function which get data from csv file and convert the data to array. After receiving it as array,calling a function flatter_array converted multidimensional array to single dimension array.

Now, foreach loop to insert one by one data as term into WordPress.

global $wpdb;
$array_data = csv_to_array('csv_key.csv');

foreach($array_data as $data)
        $data = trim($data);
    if($data != '')
             //Replace  with your desired taxonomy.
              $term_exists = term_exists($data, ‘’);
              if ($term_exists !== 0 && $term_exists !== null) {

                   echo $term_exists->name." exists!
"; }else { $term = wp_insert_term($data, ''); echo $term->name." inserted Successfully
"; } } } // function to convert csv data into array function csv_to_array($filename='', $delimiter=',') { if(!file_exists($filename) || !is_readable($filename)) return FALSE; $header = NULL; $data = array(); if (($handle = fopen($filename, 'r')) !== FALSE) { while (($row = fgetcsv($handle, 1000, $delimiter)) !== FALSE) { array_push($data,$row); } fclose($handle); } return flatter_array($data); } // To convert multi dimensional array to single dimension function flatter_array($array) { if (!is_array($array)) { return FALSE; } $result = array(); foreach ($array as $key => $value) { if (is_array($value)) { $result = array_merge($result, array_flatten($value)); } else { $result[$key] = $value; } } return $result; }

That’s it place above code into the file, your CSV file and replace with your desired taxonomy in wp_insert_term function.

Also Read:

Disable updates for specific plugin in WordPress
To get Last auto increment id in WordPress
To import user from CSV in WordPress

Hope this helps someone else out.As always, thanks for reading. Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

MySQL Transaction tutorial for Beginners

Posted on 7 years ago


Hello world

Updated 1 month ago

Nick Carter

In depth Understanding of Laravel Core

Updated 3 months ago


ECMAScript 6 New Features – 2

Updated 1 year ago


The Reader’s Poll – June 2015

Posted on 4 years ago


The Reader’s Poll – August 2014

Posted on 5 years ago


The Reader’s Poll – June 2014

Posted on 5 years ago


The Readers’ Poll – May 2014

Posted on 5 years ago


The Readers’ Poll – August 2013

Posted on 6 years ago