Set WordPress user password by user login with PHP

/**
 * Set user password by user login.
 *
 * Usage example: `wpcodebook_set_password( 'admin', 'pass' )`
 *
 * @see https://wpcodebook.com/set-wordpress-user-password-by-login-php/
 * @see https://developer.wordpress.org/reference/functions/wp_set_password/
 * @see https://developer.wordpress.org/reference/functions/get_user_by/
 */
function wpcodebook_set_password( $username, $password ) {
	if ( $user = get_user_by( 'login', $username ) ) {
		wp_set_password( $password, $user->ID );
		return true;
	}
	return false;
}

Leave a Comment