wp_update_user PHP function does not work
-
Hi gurus,
Use case: I want to create a user using the elementor pro form which includes mandatory fields for creating a user plus name, last name role/profile and one custom field created with ACF (dni).
The issue: With this piece of PHP code the user registration (wp_create_user) works perfectly however the second part (wp_update_user) doesn’t do anything. No data is updated but there is not error neither.
This is the code:
add_action( 'elementor_pro/forms/new_record', 'thewpchannel_elementor_form_create_new_user' , 10, 2 ); function thewpchannel_elementor_form_create_new_user($record,$ajax_handler) { $form_name = $record->get_form_settings('form_name'); //Check that the form is the "create new user form" if not - stop and return; if ('Create New User' !== $form_name) { return; } $form_data = $record->get_formatted_data(); $username=$form_data['Username']; //Get the value of the input with the label "User Name" $password = $form_data['Password']; //Get the value of the input with the label "Password" $email=$form_data['Email']; //Get the value of the input with the label "Email" $user = wp_create_user($username,$password,$email); // Create a new user, on success return the user_id no failure return an error object if (is_wp_error($user)){ // if there was an error creating a new user $ajax_handler->add_error_message("Failed to create new user: ".$user->get_error_message()); //add the message $ajax_handler->is_success = false; return; } $first_name=$form_data["First Name"]; //Get the value of the input with the label "First Name" $last_name=$form_data["Last Name"]; //Get the value of the input with the label "Last Name" $dni=$form_data["dni"]; //Get the value of the input with the label "dni" $perfil=$form_data["perfil"]; //Get the value of the input with the label "perfil" $update = wp_update_user(array("ID"=>$user,"first_name"=>$first_name,"last_name"=>$last_name,"role"=>$perfil,"dni_cif_socio"=>$dni)); // Update the user with the first name and last name and dni and profile // OLD $update = wp_update_user(array("ID"=>$user,"user_pass"=>$password,"first_name"=>$first_name,"last_name"=>$last_name)); // Update the user with the first name and last name if (is_wp_error($update)){ // if there was an error updating user $ajax_handler->add_error_message("Failed to update user: ".$update->get_error_message()); //add the message $ajax_handler->is_success = false; return; } }
Thank you in advance,
Carlos
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- The topic ‘wp_update_user PHP function does not work’ is closed to new replies.