Import CSV Data To WordPress Using Custom Plugin

Import CSV Data To WordPress Using Custom Plugin

This post was originally created on our sister site WP Cover.
View Original Article
November 30, 2018

When you start working with large WordPress sites you frequently need to update mass amounts of data.  Or in some cases add mass amounts of data.  The easiest way to do this is normally through spreadsheets.  There are a handful of plugins out there that handle common CSV importing and exporting but I’ve found many of them to be confusing, bloated and/or require different add-ons to be purchased depending on the task you need to complete.

This morning I had a task of updating every WordPress term within the custom taxonomy of ‘location’ with latitude and longitude data.  I already had the term ID, latitude and longitude stored in a neat spreadsheet but my searches for a pre-existing plugin that could handle the updating was getting me nowhere.  I’m not saying there’s not a plugin out there that will do this, but I didn’t feel like spending two hours researching, installing, re-formatting spreadsheets, receiving errors, etc. when I knew I could write my own custom plugin much quicker.  And as a bonus, it would give me a nice article to share with you.

Here It Goes:

As is the case with most plugins I create, I start with my handy framework for building a plugin to do ‘Random’ things.  From there, I modified the plugin to handle my CSV import, process the file and loop through the rows of data (beginning with row 2).  As you will see from the code, I had three rows of data in my spreadsheet, the first being the term ID, the second being the latitude coordinate and the third being the longitude coordinate.  Here is the file I uploaded in if you want to check it out.  We’re leveraging Advanced Custom Fields update_field function to update the term data within the loop.  Therefore, you’ll need to ensure that plugin is installed and activated as well.

Here is the full plugin code:

 

Import CSV Custom

Click Process button to do whatever is below in your run process.

1 && $i < 302) { $imported++; //start new import $locationID = sanitize_text_field($line_of_text[0]); $locationLat = sanitize_text_field($line_of_text[1]); $locationLng = sanitize_text_field($line_of_text[2]); $refpost = 'location_' . $locationID; update_field( 'latitude', $locationLat, $refpost ); update_field( 'longitude', $locationLng, $refpost ); echo 'Updated Term ID ' . $locationID . ' - ' . $locationLat . ', ' . $locationLng . '
'; } } } fclose($file_handle); echo 'Updated ' . $imported . ' terms'; } else { echo 'Fail'; } } ?>

Hope this helps somebody else with quick CSV imports. The base of this code could be tweaked in many ways to perform an infinite number of tasks on a WordPress database.

The post Import CSV Data To WordPress Using Custom Plugin appeared first on WP Cover.

Share Your Thoughts

FREE: 10 Step Guide To Choosing A Web Design Company

FREE: 10 Step Guide To Choosing A Web Design Company

Download your free e-book. Learn the questions you should be asking prior to partnering with a web design company. Enter your email address and we'll send it to you right away!

We've sent the e-book to your email address!