How to Reset Passwords for All or Selected Users in WordPress

In this tutorial, you will learn how to change passwords for multiple users in WordPress using a code snippet. This method is useful when you need to update passwords for a large number of users at once, saving you time and effort. We’ll walk you through the steps to implement the code snippet, generate new passwords, and send email notifications to users with their updated credentials.

Before proceeding with any customizations in WordPress, it’s essential to set up a child theme. A child theme acts as a safe and efficient way to make modifications without affecting the parent theme. If you haven’t set up a child theme yet, follow this tutorial on How to Create a Child Theme for Customization. It will guide you through the process and ensure that your customizations remain intact even after theme updates.

Add a Custom Function

To get started, locate and open the functions.php file of your WordPress child theme. Copy the following code snippet and paste it into the functions.php file:

function change_all_user_passwords_once() {
	// Run this code snippet only once by setting a flag in the WordPress database
	$password_changed = get_option( 'passwords_changed' );
	if ( $password_changed ) {
		return; // Passwords already changed, exit the function
	}

	$users = get_users();

	foreach ( $users as $user ) {
		$new_password = wp_generate_password( 12, true );
		wp_set_password( $new_password, $user->ID );

		// Send email notification to users with their new passwords
		wp_mail( $user->user_email, 'Your New Password', 'Your new password: ' . $new_password );
	}

	// Update the flag to indicate that passwords have been changed
	update_option( 'passwords_changed', true );
}

// Hook the function to a specific action, such as 'init' or 'admin_init'
add_action( 'init', 'change_all_user_passwords_once' );
Code language: PHP (php)

Extra: Exclude the admin and editor roles To exclude the admin and editor roles from password changes, we can modify the code snippet. Add the following line of code just before the foreach loop:

$users = get_users(
	array(
		'role__not_in' => array( 'administrator', 'editor' ), // Skip password change for admin and editor roles
	)
);
Code language: PHP (php)

Save the changes

Save the functions.php file after adding the code snippet.

Run the code snippet

To run the code snippet and change passwords for all users, visit any page of your WordPress site. The code will execute when the associated action, such as ‘init’ or ‘admin_init’, is triggered. Note that the passwords will be changed only once.

Verify the password changes

After the code snippet has executed, you can verify that the passwords for all users have been changed. You can also test the email notifications sent to users with their new passwords.

Remove the code snippet

Once you have confirmed that the passwords have been changed successfully, it’s important to remove the code snippet from the functions.php file. This ensures that passwords won’t be reset again when the associated action is triggered.

That’s it! You have learned how to change passwords for all users in WordPress using a code snippet. By following the steps outlined above, you can efficiently update passwords for a large number of users, saving time and improving security. Remember to remove the code snippet from the functions.php file after the password changes have been made.

Leave your feedback and help us improve 🐶

We hope you found this article helpful! If you have any questions, feedback, or spot any errors, please let us know in the comments. Your input is valuable and helps us improve. If you liked this article, please consider sharing it with others. And if you really enjoyed it, you can show your support by buying us a cup of coffee ☕️ or donating via PayPal 💰.

More free knowledge, because why not?

Your thoughts matter, leave a reply 💬

Your email address will not be published. Required fields are marked *