wppaste
WordPress

get_password_reset_key( WP_User $user ): string|WP_Error

Since
4.4.0
Source
wp-includes/user.php:2935
Creates, stores, then returns a password reset key for user.

Compatibility

WordPress
since 4.4.0
PHP
7.4–8.6-dev
  • 6.7.7
  • 6.8.8
  • 6.9.7
  • 7.0.4
  • 7.1.0

Present in every tracked release (6.7.7 to 7.1.0), and compiles on PHP 7.4 through 8.6-dev.

Parameters

$userWP_User
User to retrieve password reset key for.

Return value

string|WP_Error
Password reset key on success. WP_Error on error.

Performance profile

How much work a call to get_password_reset_key() does, and what it touches: the algorithmic scaling, the Zend instruction count per call across PHP versions, the hooks it hands control to, and the core code that calls it. Measured from the compiled opcodes, not a stopwatch, so every number is identical on any machine running the same PHP version, and every function in core is ranked by cost.

Cost class
Expensive

Sends mail via wp_mail().

Scaling
Constant

No loop in the body: the same number of instructions runs whatever you pass in.

Instructions
12–71

Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 89.

Plugin surface
3 hooks

Third-party callbacks on 'retreive_password', 'retrieve_password', 'retrieve_password_key' run inside this call, and their cost is not bounded by anything here.

Called by
2

2 places in core call this, so the cost is paid more often than your own code shows.

What it touches

  • hookthird-party callbacksdo_action_deprecated()called directly
  • optionoption read or writeget_option()one call below get_password_reset_key()
  • mailsends mailwp_mail()one call below get_password_reset_key()

Further down the call graph this can also reach query, transient, cache and serialize. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.

What one call costs · 4 distinct outcomes

One number would be a lie: the work depends on which branch runs. These are every distinct cost get_password_reset_key() can have, taken from its control-flow graph on PHP 8.5.

WhenInstructionsCalls it makes
!($user instanceof)12__()
$user instanceof && is_wp_error()28do_action(), wp_is_password_reset_allowed_for_user(), is_wp_error()
$user instanceof31do_action(), wp_is_password_reset_allowed_for_user(), __(), do_action_deprecated()
$user instanceof && !is_wp_error()61–71do_action(), wp_is_password_reset_allowed_for_user(), is_wp_error(), wp_generate_password(), do_action(), time(), ->HashPassword(), ID(), is_wp_error()

Across PHP versions

Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 89 instructions, 12–71 executed per call, 5 branches. The work does not change between versions.

An instruction is not a fixed amount of time, so a matching count is not necessarily the same speed; what it rules out is a difference in the work itself.

Hooks and filters fired · 3

3 hooks fire while get_password_reset_key() runs, in this order:

  1. do_action( retreive_password )action_deprecatedline 2952 (+17 into the body)

    Fires before a new password is retrieved.

  2. do_action( retrieve_password )actionline 2961 (+26 into the body)

    Fires before a new password is retrieved.

  3. do_action( retrieve_password_key )actionline 2981 (+46 into the body)

    Fires when a password reset key is generated.

Uses · 9

Used by · 2

Source code

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 ); 	$password_reset_allowed = wp_is_password_reset_allowed_for_user( $user );	if ( ! $password_reset_allowed ) {		return new WP_Error( 'no_password_reset', __( 'Password reset is not allowed for this user' ) );	} elseif ( is_wp_error( $password_reset_allowed ) ) {		return $password_reset_allowed;	} 	// 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;}

Changelog

Introduced in 4.4.0. Unchanged from 6.7.7 through 7.1.0.

  1. 6.7.7
  2. 6.8.8
  3. 6.9.7
  4. 7.0.4
  5. 7.1.0

Signature, return type and hooks compared across 5 parsed releases.

About this page

Parsed data
Generated from the wordpress-develop 6.7.7 tag, from src/wp-includes/user.php, and regenerated for each WordPress release so it tracks the code rather than a snapshot of it.
Corrections
Something wrong on this page? Report it and it gets fixed in the next regeneration.