• 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)
  • @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.

    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="[email protected]";
        $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://www.ads-software.com/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.