WooCommerce中wp_set_password函数的等价物是什么?

问题描述 投票:2回答:1

我发现我可以自定义wp_set_password函数并将我的代码放入其中。但它只在用户通过/wp-login.php注册时执行。

这是我的代码:

function wp_set_password( $password, $user_id ) {
    // Keep original WP code
    global $wpdb;

    $hash = wp_hash_password( $password );
    $wpdb->update(
        $wpdb->users,
        array(
            'user_pass'           => $hash,
            'user_activation_key' => '',
        ),
        array( 'ID' => $user_id )
    );

    wp_cache_delete( $user_id, 'users' );

    // and now add your own
    $custom_hash = password_hash( $password, PASSWORD_DEFAULT );
    update_user_meta($user_id, 'user_pass2', $custom_hash);
}

但是我安装了WooCommerce,关于密码的所有三个主要任务是:

  • 注册,
  • 资料更新,
  • 重设密码。

所以这段代码对我没有帮助,我在WooCommerce中搜索了类似的功能,但我找不到它。无论如何,我可以在我的自定义插件中编辑这样的WooCommerce,这样做的功能是什么?

php wordpress woocommerce passwords account
1个回答
1
投票

您应该总是避免覆盖任何核心文件,因为当WordPress更新时您将丢失更改,并且您可以在这个相关的敏感过程中遇到大麻烦。

在Woocommerce中,相当于WordPress wp_set_password()的是WC_Customer set_password()方法。

要使其可插拔,您可以使用位于[WC_Customer_Data_Store][4]方法中的update()类相关钩子:

  • 在用户创建时,对于“用户注册”,请使用woocommerce_new_customer动作挂钩。
  • 在用户更新事件上,使用woocommerce_update_customer动作挂钩
  • 在“用户注册”(用户创建)上,您可以使用woocommerce_new_customer动作钩子
  • 在我的帐户>帐户详细信息部分更改/保存密码时,您也可以使用woocommerce_save_account_details操作挂钩。
  • 密码重置后,您可以使用woocommerce_customer_reset_passwor动作挂钩。

WC_Customer set_password()方法的示例用法:

// Get the WC_Customer instance object from the user ID
$customer = new WC_Customer( $user_id );

// Set password
$customer->set_password( $password );

// Save to database and sync
$customer->save();
© www.soinside.com 2019 - 2024. All rights reserved.