Create users by importing from CSV, with User ID assigned from CSV

No, there’s no way to do this inside the WordPress codebase and calls such as wp_insert_user, because as mentioned the user ID is set automatically by the database because the ID field in wp_users is an AUTOINCREMENT field in WordPress.

Therefore, to do what you want you have a couple of options:

1. Update as you go with a manual SQL statement to change the ID

Insert row by row into wp_users with wp_insert_user, and update the user ID as you go. For example, in pseudocode this would look like:

  • Get a row from your CSV
  • Insert into WordPress with e.g. wp_insert_user()
  • Get the ID of the new user
  • Run an SQL query to manually change that ID on wp_users table **and making sure it’s updated everywhere else such as wp_usermeta, wp_options where WP might have already inserted some rows.

To do this you should probably set the AUTOINCREMENT value on this table to a sensible value that is larger than any of the ID’s you will be inserting. This avoids ID collisions.

2. Insert directly into wp_users

You can’t insert data directly into wp_users with SQL except you have to understand very well the meaning of all the fields, which is beyond what I can answer here, and for that reason this is tricky and you need to be very careful if you do that. E.g. you can do this:

    insert into wp_users (ID, user_login, user_pass, user_nicename, user_email, user_url, user_registered, user_activation_
key, user_status, display_name) 
    values (12345, 'foo', 'bar', 'nicename', '[email protected]', 'http://url.com', '00-00-00', 'key', 0, 'display_name');

BUT, this is dangerous and complicated because you must figure out for yourself correct values for these fields according WordPress’s internal use of these fields, specifically:

3. Change the ID of the content instead

Change the user ID’s in your content instead. A possibly better way to achieve what you want is this:

  • Read a line from your CSV
  • Insert user with e.g. wp_insert_user
  • Get the new WP ID
  • **Record in a list somewhere (e.g. in a new database table) a lookup table of the WP User ID and the old ID from your CSV

Now when you insert any content for this user into WordPress you can use this lookup table to change the user id field to the correct new WordPress ID for that user.

This choice is better, even if you already inserted all the content due to the complexity of figuring out everything about how WordPress processes new users.

Setting the next automatic user ID / AUTOINCREMENT value in wp_users table:

However you’re doing what you’re doing, you may want to think about changing the ID AUTOINCREMENT field on wp_users to avoid ID collisions. You can do that with:

ALTER TABLE wp_users AUTO_INCREMENT = 1000000;