Skip to content
Advertisement

wordpress wp_insert_user function

I am in the process of creating a custom registration form for WordPress.
My problem is adding custom user meta. I believe the function wp_insert_user will only allow you to add the default fields within the WordPress user table.

Here is my current test code:

$username = '12344';
$password = '1111';
$user_data = array(
'ID' => '',
'user_pass' => $password,
'user_login' => $username,
'display_name' => $loginName,
'first_name' => $firstName,
'last_name' => $lastName,
'role' => get_option('default_role') ,
'user_secondry_email' => 'test@tst.com'// Use default role or another role, e.g. 'editor'
);
$user_id = wp_insert_user( $user_data );
wp_hash_password( $password );

I have found the add_user_meta function, but this requires an ID to add the metadata. Obviously the user hasn’t been created yet so they won’t have an ID. Any ideas on how to get around this?

Thanks, Dan

Advertisement

Answer

For what I understand from the WordPress documentation, the ID field is optional.

If it is present, the user is updated, if not, it is created and the ID of the new user is returned by the function.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement