问题
Im trying to change wp_set_password
pluggable function and add custom action to it:
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);
}
I put this code in my custom plugin, but it doesnt run the custom action I wrote inside of it. I'm not sure what the problem is.
Maybe I put it in on the wrong location or I should call it somewhere?
How to hook in wp_set_password()
WordPress function with WooCommerce?
Edit
This code doesn't fire at all, I tried to put the same password in users table but it doesn't care about my code and do the default action.
Edit 2
I comment the code in the plugin and changed the main pluggable.php
file in the wp-includes
folder, and added these 2 lines.
$custom_hash = $password;
update_user_meta($user_id, 'user_pass2', $custom_hash);
But it still doesn't work.
Edit 3
I even removed the whole function from pluggable.php
, It still works! I have created a username and password for new users.
It should be WooCommerce registration. I use WooCommerce login system.
Edit 4
I used the WordPress registration system /wp-login.ph
p and this code works finally.
Now I'm wondering regarding WooCommerce where and how I could achieve something like this, updating the wp_usermeta
table with something custom.
回答1:
You should always avoid to overwrite any core file, as you will loose your changes when WordPress will be updated and you can make big trouble in this related sensible processes.
You could try to use the WC_Customer() Class available setters methods like:
// Get an instance of the WC_Customer Object from the user ID
$customer = new WC_Customer( $user_id );
// Set password
$customer->set_password( $password );
// Set other metadata
$customer->set_first_name( $first_name );
$customer->set_last_name( $last_name );
// Save to database (and sync cached data)
$customer->save();
You can use the Woocommerce related hooks from
WC_Customer_Data_Store
Class:
- For user creation (or registration) located in WC_Customer_Data_Store create() method
- action hook
woocommerce_new_customer
- filter hook
woocommerce_update_customer_args
- For user update located in WC_Customer_Data_Store update() method
- action hook
woocommerce_update_customer
- filter hook
woocommerce_update_customer_args
Addition - Create a customer with Woocommerce:
1) You can use wc_create_new_customer() function that returns the user ID.
2) Or You can use an empty instance of the WC_Customer
Object using on it any setters available methods (and it will return at the end the User ID):
// Get an empty instance of the WC_Customer Object
$customer = new WC_Customer();
// Set username and display name
$customer->set_username( $user_name );
$customer->set_display_name( string $display_name )
// Set user email
$customer->set_email( $user_email );
$customer->set_display_name( $display_name );
// Set First name and last name
$customer->set_first_name( $first_name );
$customer->set_billing_first_name( $first_name );
$customer->set_last_name( $last_name );
$customer->set_billing_last_name( $last_name );
// Set password
$customer->set_password( $password );
// Set other metadata
$customer->set_billing_first_name( $first_name );
$customer->set_billing_last_name( $last_name );
// Save to database - returns the User ID (or a WP Error)
$user_id = $customer->save();
来源:https://stackoverflow.com/questions/55445695/how-to-hook-in-wp-set-password-wordpress-function-with-woocommerce