Please disable ad blocker to see the page.

Two Ways To Import CSV Data Into WordPress

So after changing your website to WordPress, you want to import its database. CSV will make it simple. How?

Look, there are two ways to import CSV data into WordPress. First via plugin and second via your own PHP code. We will analyze these two methods closely, so that you can pick any one as per your requirement. To explain it in detail I am taking an example of user's data.

Import Users from CSV Plugins: It is easy to use, only need to create a CSV according to the given sample.



By default, it provides the facility to import below 9 fields.

user_login
user_email
user_pass
first_name
last_name
display_name
role
custom_usermeta_1
custom_usermeta_2

To import more information, just enter more fields in CSV . Now here a question arise that how can you get the field-name?

 


Simply search for field-name in user's database as shown below for website.


After preparing your sheet, open "Import from CSV" page from "Users" menu.


Now upload the CSV file and click on Import button. That's it, you have successfully imported the user's data into WordPress.

Now it's time to code a script. It's a good solution to get what you need. The steps are below:

First of all create a separate php file

Then add a form to upload your CSV .

<form method="post" enctype="multipart/form-data">     <input type="file" name="users" />     <input type="submit" value="Upload" /> </form>

After that include the below file to load all WordPress functions.

$base_dir = dirname((__FILE__)); require_once($base_dir."/wp-load.php");

Thereafter define a global object variable, it will use to interact with the database.

global $wpdb;


In addition, declare two another variables as below:

$insert = 0;
$update = 0;


Then to get uploaded file and to read all CSV data use below code.

while (($data = fgetcsv($handle, 1000, ",")) !== FALSE){


After getting all the information, insert them into database.

$user_id = wp_insert_user(array(
                    'user_login' => $data[1],
                    'user_pass'  => 'password',
                    'user_email' => $data[4],
                    'role'       => 'user', // Subscriber , Contributor, Author, Editor, and Administrator.
                    'user_nicename' => $data[3],
                    'display_name' => $data[9]

$insert++;
}


Query inside while loop will insert every line of data into the user's table.

That's it, once imported you will find all users in Dashboard. 

 Click here to get the complete code.

Overall, the plugin is useful if you know the requirement and a plugin for it else code it and make your own importer. With the same way you can import articles, categories, comments etc.
Previous
Next Post »
1 Comment
avatar

I expected something on importing any data not just user data. The title and intro is misleading in that respect.

We can use CSV 2 POST to import any .csv file and create thousands of posts or pages or even custom post types. So data import into WordPress from .csv files is a big topic. Thats all.

Balas