简体   繁体   English

更改电子邮件时更新用户名 (User_Login) (WooCommerce)

[英]Update Username (User_Login) on Email Change (WooCommerce)

A number of questions have been raised on StackOverflow in regards to using email address as username when registering via My Account .在通过My Account注册时使用电子邮件地址作为用户名在 StackOverflow 上提出了许多问题。 I have the working code included below.我有下面包含的工作代码。

$this->loader->add_filter('pre_user_login', $plugin_public, 'audp_wc_register_email_as_username' );

public function audp_wc_register_email_as_username( $user_login ) {

    if ( isset ( $_POST['email'] ) ) {

        $user_login = $_POST['email'];

    }

    return $user_login;

}

I've updated my own question with the following code to update the user_login details.我已经使用以下代码更新了我自己的问题以更新 user_login 详细信息。

$this->loader->add_action( 'woocommerce_save_account_details', $plugin_public, 'audp_wc_update_email_and_username', 20, 1 );

public function audp_wc_update_email_and_username() {

    if ( isset( $_POST['account_email'] ) ) {

        global $wpdb;

        $user_id = get_current_user_id();
        $new_login = $_POST['account_email'];

        // Update user user_login
        $wpdb -> update( $wpdb -> users, 
            array( 'user_login' => $new_login, 'user_nicename' => $new_nicename ), 
            array( 'ID' => $user_id )
        );      
    }   
    // Update nickname
    update_user_meta( $user_id, 'nickname', $new_login );
}

I've updated the question to include the full code.我已经更新了问题以包含完整的代码。 Because we are dealing with user_login we cannot use wp_update_user(), I have used the class description above, but the below code is standard (non-class) functions.因为我们正在处理 user_login 我们不能使用 wp_update_user(),我使用了上面的类描述,但下面的代码是标准(非类)函数。

add_action( 'woocommerce_save_account_details', 'audp_wc_update_email_and_username', 20, 1 );
function audp_wc_update_email_and_username() {

if ( isset( $_POST['account_email'] ) ) {

    global $wpdb;

    $user_id = get_current_user_id();
    $new_login = $_POST['account_email'];

        // Update user user_login
        $wpdb -> update( $wpdb -> users, 
            array( 'user_login' => $new_login, 'user_nicename' => $new_nicename ), 
            array( 'ID' => $user_id ) 
        );

    // Update nickname
    update_user_meta( $user_id, 'nickname', $new_login );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查 WordPress 中是否存在 user_login 并通过附加到现有 user_login 以编程方式创建新用户 - How to check if existing user_login in WordPress and create new user programatically by appending to an existing user_login 新表字段上的Wordpress user_login字段 - Wordpress user_login field on new table field WordPress 数据库错误:[表 'databasename.wp_users' 不存在] SELECT * FROM wp_users WHERE user_login = 'Username' - WordPress Database Error: [Table 'databasename.wp_users' doesn't exist] SELECT * FROM wp_users WHERE user_login = 'Username' 电子邮件订单上的Woocommerce用户名 - Woocommerce username on email order 在WordPress中使用用户名或电子邮件登录 - login with username or email in wordpress 将我的user_login从数据库加载到我的acf选择字段代码中 - load my user_login from database into my acf select field code 为什么wordpress不为`wp_users`.`user_login`创建唯一索引? - Why doesn't wordpress create an unique index for `wp_users`.`user_login`? 仅使用用户名登录的用户 - Login user with username only 如何更改主要的WooCommerce帐户用户/电子邮件? - How do I change the main WooCommerce account user/email? WooCommerce / Wordpress-自动使用整个电子邮件作为用户名? - WooCommerce / Wordpress - automatically use the entire email as username?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM