Search
Close this search box.
Search

How to Bulk Import Users to WordPress using a CSV file

Listen to this article

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. 😀

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! 😀

[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!

Harish Kashyap

Harish Kashyap

One Response

  1. Hi there – If I do this to add a bunch of new users and don’t specify a password, one will be created – correct? How is the user notified of their password? Does WP automatically send the login information to the new users created?

    Thanks,

Leave a Reply

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

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

WordPress Development

Custom WordPress Solutions, for You

LearnDash Development

Custom LearnDash Solutions, for You

WooCommerce Development

Custom WooCommerce Solutions, for You

WordPress Plugins

Scale your WordPress Business

WooCommerce Plugins

Scale your WooCommerce Business

LearnDash Plugins

Scale your LearnDash Business

Label

Title

Subtext

Label

Title

Subtext

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads