WordPress at a glance

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.

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

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

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

Works based on: wp_hash_password()

No Hooks.

Return

Nothing (null).

Usage

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

Examples

#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 ); }
	);

}

Code of wp_set_password: wp-includes/pluggable.php VER 5.0.1

<?php
function wp_set_password( $password, $user_id ) {
	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');
}

Related Functions

From tag: password

More from tag: Site security (safety)

More from category: Login/Logout

No comments
    Hello, !     Log In . Register