Change WP User Password
November 13, 2024 ยท AI Conversations
User
change wp user password programmatically
Assistant
To change a WordPress user password programmatically, you can use the `wp_set_password` function. This function allows you to change the password for a specific user. Here's an example of how to do it:
$user_id = 1; // ID of the user whose password you want to change
$new_password = 'new_secure_password';
// Change the password
wp_set_password($new_password, $user_id);
This will update the password for the user with ID 1 to `'new_secure_password'`.
### Important Notes:
– After changing the password with `wp_set_password`, the user will be logged out, and they'll need to log in with the new password.
– Ensure that the new password meets the WordPress password strength requirements, or use a sufficiently complex password.
If you need to change the password for the current logged-in user, you can use:
$new_password = 'new_secure_password';
wp_set_password($new_password, get_current_user_id());
This will change the password for the currently logged-in user.