Categories
WordPress WP-CLI

Importing 1100 users into WordPress from a CSV file using WP-CLI

The Problem

A spreadsheet of ~1100 people (name, email address, etc) that needs to be imported into a WordPress site as users:

User Import CSV File
The User Import CSV File

I tried several user import plugins, but I had trouble with some users not importing (duplicate email addresses and/or usernames), and the plugins I tried didn’t make it easy to see which accounts were failing to import and why.

The import process via the WordPress dashboard was also encountering timeout problems due to the large amount of data being imported.

The Solution

WP-CLI’s user import command available to the rescue!

I used the following command to import the 1100+ user accounts:

wp user import-csv --skip-update user-import.csv

Note: this command skips user accounts that already exist because I included the --skip-update flag.

The output of this command made it very easy to identify the lines/users that were failing to import due to their email addresses already exist.

Bulk Deleting all Subscriber Accounts

I fixed up the duplicate data in the CSV file, and then used the following command to delete the imported user accounts before attempting the import again:

wp user list --role=subscriber --field=ID | xargs wp user delete --yes

Note: this command deletes ALL existing subscriber user accounts.

Once the imported accounts were deleted, I was able to retry the import using the command above.

Using WP-CLI for this ended up working great, and the import process was much quicker than using a plugin because I didn’t have to worry about server timeouts and other problems.

Are you using WP-CLI?

If you’re not already using WP-CLI, be sure to check out my previous presentation.