John Davidson

php - Update WooCommerce User programmatically when creating a user

0 comments
Message:


Im trying to create a new user programmatically trough a form but Im having problem with getting the phone numer and country set trough wp_create_user - why wont it take the values? First and last name works as expected.


Relevant code:


$user_id = wp_create_user( $username, $random_password, $user_email ); 
wp_update_user([
'ID' => $user_id,
'first_name' => rgar( $entry, '20.3' ),
'last_name' => rgar( $entry, '20.6' ),
'phone' => rgar( $entry, '16' ),
'country' => rgar( $entry, '24.6' )
]);


My Answer:
$user_id,
'first_name' => $first_name,
'last_name' => $last_name,
'phone_number' => $phone_number,
'country' => $country
]);

The issue here is that the wp_update_user function does not have built-in support for updating custom user meta fields like phone number and country. You will need to use the update_user_meta function to update these custom fields.

Here is how you can update the phone number and country fields for the user:

php
update_user_meta( $user_id, 'phone_number', $phone_number );
update_user_meta( $user_id, 'country', $country );


Make sure to replace 'phone_number' and 'country' with the actual meta keys you are using for these fields in your WordPress installation.

By using the update_user_meta function, you can update any custom user meta fields for the user created programmatically.

Rate this post

5 of 5 based on 2039 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us