I’d like to add a new user to the WordPress user table. The code for wp_create_user works fine but it is adding the user incrementally to the next available ID in the user table.
Instead, I want to create the user and assign it to a particular user ID. The user ID is already available (externally) and I want the ID entered into the WordPress database to match the external one.
$user_login = $username ;
$pas1 = $email_address;
$user_email = $email_address;
wp_create_user( $user_login, $pas1, $user_email );
$user = new WP_User($user_id);
$user->set_role('editor');
For example, I want to know how to set the known $user_id at the time of wp_create_user.
wp_insert_user
will not create a non-existing user with a set ID, it will return an error.The way to do this would be to first do:
The code above will first create an empty user row with the set ID (it will fail hard if the ID already exists, duplicate entry for primary key ) which you can then modify using the API as needed.
Not with
wp_create_user()
. Check the source:wp_create_user
doesn’t accept an ID argument. However, take a look at the function used at the bottom ofwp_create_user()
.wp_insert_user()
does honor an ID argument.The downside is that it will overwrite an existing user with the same ID, so you will need to write the logic to handle that case.