How do I update user email from frontend input field?

Question

I’m using the below function which is working for my phone and address fields. I can’t seem to change the email though… any idea what I’m doing wrong?

function um_modifications_callback() {

    // Ensure we have the data we need to continue
    if( ! isset( $_POST ) || empty( $_POST ) || ! is_user_logged_in() ) {

        // If we don't - return custom error message and exit
        header( 'HTTP/1.1 400 Empty POST Values' );
        echo 'Could Not Verify POST Values.';
        exit;
    }

    $user_id        = get_current_user_id();                            // Get our current user ID
    $um_val         = sanitize_text_field( $_POST['first_name'] );      // Sanitize our user meta value
    $um_user_email  = sanitize_text_field( $_POST['user_email'] );      // Sanitize our user email field

    $phone = $_POST['phone'];
    $address = $_POST['address'];
    $email = $_POST['user_email'];
    
    update_field('phone', $phone, 'user_'.$user_id);
    update_field('address', $address, 'user_'.$user_id);

    update_user_meta( $user_id, 'first_name', $um_val );                // Update our user meta

    wp_update_user( array(
        'ID'            => $user_id,
        'user_email'    => $email,
    ) );

    exit;
}

in progress 0
user205498 2 months 2021-09-26T22:03:56-05:00 0 Answer 0 views 0

Answer ( 1 )

    0
    2021-09-29T04:19:46-05:00

    You can diagnose error while updating user email.

    $user_data = wp_update_user( array( 'ID' => $user_id, 'user_email' => $email ) );
     
    if ( is_wp_error( $user_data ) ) {
        // There was an error; possibly this user doesn't exist.
        echo 'Error.';
    } else {
        // Success!
        echo 'User profile updated.';
    }
    

    Might it can help you.

Leave an answer

Browse
Browse