Title: wp_update_user PHP function does not work
Last modified: August 8, 2023

---

# wp_update_user PHP function does not work

 *  [carlosestero](https://wordpress.org/support/users/carlosestero/)
 * (@carlosestero)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/wp_update_user-php-function-does-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:
 *     ```wp-block-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
    -  This topic was modified 2 years, 9 months ago by [Steven Stern (sterndata)](https://wordpress.org/support/users/sterndata/).
      Reason: fixed title

Viewing 1 replies (of 1 total)

 *  [Sarathlal N](https://wordpress.org/support/users/sarathlal/)
 * (@sarathlal)
 * [2 years, 9 months ago](https://wordpress.org/support/topic/wp_update_user-php-function-does-work/#post-16956657)
 * [@carlosestero](https://wordpress.org/support/users/carlosestero/) 
   Your code
   is working without issue. I think, the form is not passing the details to be 
   updated.I have modified your code to create a user on template redirect because
   I don’t have elementor pro & don’t know your form details.
 *     ```wp-block-code
       add_action( 'template_redirect',  'thewpchannel_elementor_form_create_new_user');
       function thewpchannel_elementor_form_create_new_user()
       {
           error_log("-- Started --");
           $username= "abcdefgh"; 
           $password = "#AS2df@sdd";
           $email="hello@sample.com";
           $user = wp_create_user($username,$password,$email); // Create a new user, on success return the user_id no failure return an error object
           error_log("-- created user --");
           error_log($user);
           if (is_wp_error($user)){ // if there was an error creating a new user
               error_log("-- error happened on creation --");
           }
           $first_name="pppp";
           $last_name="llllll";
           $update = wp_update_user(array("ID"=>$user,"first_name"=>$first_name,"last_name"=>$last_name)); // Update the user with the first name and last name and dni and profile
   
           if (is_wp_error($update)){ // if there was an error updating user
               error_log("-- error happened on update --");
           }
       }
       ```
   
 * I have added enough debug points in my code and so I can understand what is happening
   in every steps. The `error_log` function write details to WordPress default debug.
   log file. We just need to enable debugging in WordPress.
   [https://wordpress.org/documentation/article/debugging-in-wordpress/](https://wordpress.org/documentation/article/debugging-in-wordpress/)
   I suggest you to add debug points in your code and understand what & which steps,
   details are missing.

Viewing 1 replies (of 1 total)

The topic ‘wp_update_user PHP function does not work’ is closed to new replies.

## Tags

 * [Registration](https://wordpress.org/support/topic-tag/registration/)
 * [user](https://wordpress.org/support/topic-tag/user/)
 * [wp_create_user](https://wordpress.org/support/topic-tag/wp_create_user/)
 * [wp_update_user()](https://wordpress.org/support/topic-tag/wp_update_user/)

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 1 reply
 * 2 participants
 * Last reply from: [Sarathlal N](https://wordpress.org/support/users/sarathlal/)
 * Last activity: [2 years, 9 months ago](https://wordpress.org/support/topic/wp_update_user-php-function-does-work/#post-16956657)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
