Technology WordPress Tips & Tricks

How to Bulk Import Users to WordPress using a CSV file

Adding users one by one is fine until you have hundreds to bring in. Here is how to bulk import users to WordPress using a CSV file, so you can onboard a whole list in one go instead of clicking forever.

Harish Kashyap Harish Kashyap 3 min read

Create-Users-WordPressIn your WordPress admin panel, if you head over to Tools -> Import, you’ll notice WordPress does provide several tools to import posts, pages, categories, tags and so on.

But there isn’t an option to import users.

The solution: Look for a plugin that let’s you handle this. :D

Honestly, if you’re not a developer, or are just looking to use this functionality, the Import users from CSV with meta plugin can help.

But, if you’re looking to provide this functionality within your theme or plugin, or as a solution to your client, then this article is for you! :D

[space]

I’m going to consider that you’ve created the user-interface to accept the CSV file. This could be in the admin panel, under the settings of your theme or plugin, or on the front-end.

Once that’s ready, you need to:

  • Validate the file
  • Read the values
  • Create users

So, here goes.

[space]

Step #1 Validating the CSV File

Now, the values present in the CSV file should be added in a pre-defined format. Basically the format in which you will be reading them.

For example, the first column should be the username (‘Username’), the second the email (‘Email’) and then the meta fields in a specific order.

To verify the format of the file, here’s what you need to do:

function wdm_validate_csv($csv_file)
{
    $requiredHeaders = array('Username', 'Email');
    $firstLine = fgets($csv_file); //get first line of the CSV file
    $fileHeader = str_getcsv(trim($firstLine), ',', "'"); //parse the contents to an array

    //check the headers of the file
    if ($foundHeaders !== $requiredHeaders) {
      // report an error
      return false;
    }

    return true;
}

You need to add the above code in your plugin or theme. The code reads the first line of the file and verifies if the headers are correct (or rather as you expect them to be).

Once the headers are okay, you can proceed to reading the values and creating users.

Step #2 Reading Values to Create Users

Well, once you’ve validated the CSV file, you can use the values to create WordPress users. For this, add the below code in your plugin or theme file:

// $file is the CSV file with the values
if (($handle = fopen($file, "r")) !== FALSE) {
    if(wdm_validate_csv($handle)) {
      while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        if (count($data)>1) {
          wdm_create_user($data[0], $data[1]);
        }
      }
    }
    fclose($handle);
}

The above code calls a wdm_create_user function to create the users. We’ll see the working of that function next!

[space]

Step #3 Import Users to WordPress

The final step of course is to create the user in WordPress.

To create a user, you need to first check if the user exists; if not, you can go ahead and create one.

Here’s the code for the same:

// create the user
function wdm_create_user($uname, $email)
{
    $user = array(
      'user_login' => $uname,
      'user_pass' => 'abc@123',
      'user_email' => $email
    );

    // check if user exists
    if (username_exists($uname)) {
      // update user or do nothing
    } else {
      // create new user
      $user_id = wp_insert_user($user);
    }
}

Do note: I’ve just added a static password here. You can generate a random password for the users. If you do not specify a password, WordPress will generate a random password for you.

[space]

That’s about it!

Your users should be created. :-)

Questions and doubts can be asked away, in the comment section below!

Get a FREE Consultation

Let's build something that lasts.

Share what's on your mind — a clear brief, a half-formed idea, or just a sense that something needs to change. We'll listen first, ask the right questions, and point you toward what's actually worth building.

We take on a handful of projects each quarter,ones where we can truly make a difference.

  • Receive a human response within 24 hours
  • Get a detailed scope and quote upfront
  • We're happy to sign an NDA upon request

    Free 30-Min Strategy Call

    Your Name *

    Your Phone No *

    Work Email *

    Your Budget*

    Project Details *