Skip to main content

Mass import users into WordPress

This page is general WordPress advice and not specific to Events Manager. It is useful when you are moving to Events Manager from another system and want your customers to exist as WordPress users first, so that bookings can belong to their accounts.

The original version of this page, from 2009, used PHP's mysql_* functions, which were removed in PHP 7, and inserted rows into the users table directly. Don't use it. The two methods below go through WordPress's own functions, which create the user's meta rows and hash passwords correctly.

Before you start

Back up the database. Run the import on a staging copy first. You need shell access with WP-CLI for both methods.

Do you need accounts at all?

Events Manager can take bookings without creating a user account for each person. See No-user bookings. If your imported customers only need their past bookings recorded, accounts may be unnecessary.

Import a CSV file with WP-CLI​

  1. Prepare a CSV file with a header row. user_login and user_email are required. Other columns are optional and use the field names of wp_insert_user().

    users.csv
    user_login,user_email,display_name,first_name,last_name,role
    aroberts,alex.roberts@example.com,Alex Roberts,Alex,Roberts,subscriber
    mchen,mei.chen@example.com,Mei Chen,Mei,Chen,subscriber
  2. Run the import:

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

    --skip-update leaves existing users untouched when a login or email is already taken. Without it, existing users are updated from the file.

  3. Check the result:

    wp user list --role=subscriber --fields=ID,user_login,user_email

Leave out a user_pass column and WordPress generates a random password for each user. People then set their own through Lost your password? on the login screen. Add --send-email only if you want WordPress to email every new user, and check first that your site can deliver that much mail.

Import with a script​

Use a script when the source is not a CSV file, or each user needs extra processing. Save this outside the web root and run it with wp eval-file import-users.php.

import-users.php
<?php
$rows = array(
array( 'login' => 'aroberts', 'email' => 'alex.roberts@example.com', 'first' => 'Alex', 'last' => 'Roberts' ),
array( 'login' => 'mchen', 'email' => 'mei.chen@example.com', 'first' => 'Mei', 'last' => 'Chen' ),
);

foreach ( $rows as $row ) {
if ( username_exists( $row['login'] ) || email_exists( $row['email'] ) ) {
WP_CLI::log( "Skipped {$row['login']}: already exists." );
continue;
}
$user_id = wp_insert_user( array(
'user_login' => sanitize_user( $row['login'], true ),
'user_email' => sanitize_email( $row['email'] ),
'user_pass' => wp_generate_password( 24 ),
'first_name' => sanitize_text_field( $row['first'] ),
'last_name' => sanitize_text_field( $row['last'] ),
'display_name' => sanitize_text_field( $row['first'] . ' ' . $row['last'] ),
'role' => 'subscriber',
) );
if ( is_wp_error( $user_id ) ) {
WP_CLI::warning( "{$row['login']}: " . $user_id->get_error_message() );
} else {
WP_CLI::log( "Created {$row['login']} as user $user_id." );
}
}

Replace the $rows array with a loop over your own source. Three rules apply whatever the source:

  • Pass a plain-text password to wp_insert_user(). It hashes the password itself, and hashing it first locks the user out.
  • Let WordPress assign user IDs. Keep a record of old ID to new ID if other imported data refers to users.
  • Running under WP-CLI avoids the time and memory limits of a web request. For tens of thousands of users, import in batches.

Test it​

  1. Run wp user list --number=5 --orderby=registered --order=DESC and confirm the newest users are the ones you imported.
  2. Use the password reset link as one of the imported users and log in.
  3. Open Users then All Users and check the count and roles.