我发现我可以自定义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,这样做的功能是什么?
您应该总是避免覆盖任何核心文件,因为当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();