On this page
get_password_reset_key( WP_User $user ): string|WP_Error
Creates, stores, then returns a password reset key for user.
Parameters
$userWP_User Required-
User to retrieve password reset key for.
Return
string|WP_Error Password reset key on success. WP_Error on error.
Source
File: wp-includes/user.php. View all references
function get_password_reset_key( $user ) {
global $wp_hasher;
if ( ! ( $user instanceof WP_User ) ) {
return new WP_Error( 'invalidcombo', __( '<strong>Error:</strong> There is no account with that username or email address.' ) );
}
/**
* Fires before a new password is retrieved.
*
* Use the {@see 'retrieve_password'} hook instead.
*
* @since 1.5.0
* @deprecated 1.5.1 Misspelled. Use {@see 'retrieve_password'} hook instead.
*
* @param string $user_login The user login name.
*/
do_action_deprecated( 'retreive_password', array( $user->user_login ), '1.5.1', 'retrieve_password' );
/**
* Fires before a new password is retrieved.
*
* @since 1.5.1
*
* @param string $user_login The user login name.
*/
do_action( 'retrieve_password', $user->user_login );
$allow = true;
if ( is_multisite() && is_user_spammy( $user ) ) {
$allow = false;
}
/**
* Filters whether to allow a password to be reset.
*
* @since 2.7.0
*
* @param bool $allow Whether to allow the password to be reset. Default true.
* @param int $user_id The ID of the user attempting to reset a password.
*/
$allow = apply_filters( 'allow_password_reset', $allow, $user->ID );
if ( ! $allow ) {
return new WP_Error( 'no_password_reset', __( 'Password reset is not allowed for this user' ) );
} elseif ( is_wp_error( $allow ) ) {
return $allow;
}
// Generate something random for a password reset key.
$key = wp_generate_password( 20, false );
/**
* Fires when a password reset key is generated.
*
* @since 2.5.0
*
* @param string $user_login The username for the user.
* @param string $key The generated password reset key.
*/
do_action( 'retrieve_password_key', $user->user_login, $key );
// Now insert the key, hashed, into the DB.
if ( empty( $wp_hasher ) ) {
require_once ABSPATH . WPINC . '/class-phpass.php';
$wp_hasher = new PasswordHash( 8, true );
}
$hashed = time() . ':' . $wp_hasher->HashPassword( $key );
$key_saved = wp_update_user(
array(
'ID' => $user->ID,
'user_activation_key' => $hashed,
)
);
if ( is_wp_error( $key_saved ) ) {
return $key_saved;
}
return $key;
}
Hooks
- apply_filters( 'allow_password_reset',
bool $allow ,int $user_id ) -
Filters whether to allow a password to be reset.
- do_action_deprecated( 'retreive_password',
string $user_login ) -
Fires before a new password is retrieved.
- do_action( 'retrieve_password',
string $user_login ) -
Fires before a new password is retrieved.
- do_action( 'retrieve_password_key',
string $user_login ,string $key ) -
Fires when a password reset key is generated.
Related
Uses
| Uses | Description |
|---|---|
| do_action_deprecated() wp-includes/plugin.php | Fires functions attached to a deprecated action hook. |
| wp_generate_password() wp-includes/pluggable.php | Generates a random password drawn from the defined set of characters. |
| wp_update_user() wp-includes/user.php | Updates a user in the database. |
| is_user_spammy() wp-includes/ms-functions.php | Determines whether a user is marked as a spammer, based on user login. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| is_multisite() wp-includes/load.php | If Multisite is enabled. |
| do_action() wp-includes/plugin.php | Calls the callback functions that have been added to an action hook. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| is_wp_error() wp-includes/load.php | Checks whether the given variable is a WordPress Error. |
| WP_Error::__construct() wp-includes/class-wp-error.php | Initializes the error. |
Used By
| Used By | Description |
|---|---|
| retrieve_password() wp-includes/user.php | Handles sending a password retrieval email to a user. |
| wp_new_user_notification() wp-includes/pluggable.php | Emails login credentials to a newly-registered user. |
Changelog
| Version | Description |
|---|---|
| 4.4.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_password_reset_key