wp_set_password()修改某个指定用户的密码

目录

描述

Updates the user's password with a new encrypted one.

For integration with other applications, this function can be overwritten to
instead use the other package password checking algorithm.

wp_set_password() 描述

用法

<?php wp_set_password$password$user_id ?>

wp_set_password() 用法

参数

$password

(string) (必填) The plaintext new user password

默认值: None

$user_id

(integer) (必填) User ID

默认值: None

wp_set_password() 参数

示例

Below is an example showing how to update a user's password

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

<?php
$user_id = 1;
$password = 'HelloWorld';
wp_set_password( $password, $user_id );
?>

Please note: This code should be deleted after ONE page load, otherwise the password will be reset on every subsequent load, sending the user back to the login screen each time.

wp_set_password() 示例

注意

wp_set_password() 注意

历史

添加于 版本: 2.5

wp_set_password() 历史

源文件

wp_set_password() 函数的代码位于 wp-includes/pluggable.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Updates the user's password with a new encrypted one.
 *
 * For integration with other applications, this function can be overwritten to
 * instead use the other package password checking algorithm.
 *
 * 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.
 *
 * @since 2.5.0
 *
 * @global wpdb $wpdb WordPress database abstraction object.
 *
 * @param string $password The plaintext new user password
 * @param int    $user_id  User ID
 */
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');
}
endif;

if ( !function_exists( 'get_avatar' ) ) :

wp_set_password() 源文件

相关