WordPress at Your Fingertips

wp_set_password() WP 2.5.0

Updates the user's password with a new encrypted one. Updates the specified password in the database and resets the user's cache.

Please note: This function should be used sparingly and is really only meant for single-time application. Leveraging this improperly in a plugin or theme could result in an endless loop of password resets if precautions are not taken to ensure it does not execute on every page load.

This is a pluggable function, and it can be replaced by a plugin. It means that this function is defined (works) only after all plugins are loaded (included), but before this moment this function has not defined. Therefore, you cannot call this and all functions depended on this function directly from a plugin code. They need to be called on plugins_loaded hook or later, for example on init hook.

Function replacement (override) — in a plugin you can create a function with the same name, then it replace this function.

No Hooks.


null. Nothing.


wp_set_password( $password, $user_id );
$password(string) (required)
The plaintext new user password
$user_id(int) (required)
User ID


#1 Example of password change (reset) via PHP

Paste the following code into the theme file functions.php, and then go to any page of the site and add to the URL ?set_pass_for=mylogin to activate the function. As a result, the password of the mylogin user will be changed to resetpass. You can then delete the code.

if( isset($_GET['set_pass_for']) ){

	add_action('init', function () {
		$user = get_user_by( 'login', $_GET['set_pass_for'] );
		wp_set_password( 'resetpass', $user->ID ); }



  • Global. wpdb. $wpdb WordPress database abstraction object.

Code of wp_set_password() WP 5.8.3

function wp_set_password( $password, $user_id ) {
	global $wpdb;

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

	clean_user_cache( $user_id );


Site security (safety)


vladlu 100vlad.lu
Editors: Kama 101
No comments
    Log In