如何从前端输入字段更新用户电子邮件?

时间:2021-09-27 作者:user205498

我正在使用下面的功能,它适用于我的电话和地址字段。我似乎无法更改电子邮件。。。知道我做错了什么吗?

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;
}

1 个回复
SO网友:Maulik Paddharia

您可以在更新用户电子邮件时诊断错误。

$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.\';
}
也许它能帮助你。