get_password_reset_key( WP_User $user ): string|WP_Error
- Since
- 4.4.0
- Source
wp-includes/user.php:3010
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
- Scaling
- Constant
- Instructions
- 11–57
- Plugin surface
- 3 hooks
- Called by
- 2
Sends mail via wp_mail().
No loop in the body: the same number of instructions runs whatever you pass in.
Executed per call on PHP 8.5, depending on the branch taken. The body compiles to 75.
Third-party callbacks on 'retreive_password', 'retrieve_password', 'retrieve_password_key' run inside this call, and their cost is not bounded by anything here.
2 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
do_action_deprecated()called directly - optionoption read or write
get_option()one call below get_password_reset_key() - mailsends mail
wp_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.
| When | Instructions | Calls it makes |
|---|---|---|
!($user instanceof) | 11 | __() |
$user instanceof && is_wp_error() | 27 | do_action(), wp_is_password_reset_allowed_for_user(), is_wp_error() |
$user instanceof | 30 | do_action(), wp_is_password_reset_allowed_for_user(), __(), do_action_deprecated() |
$user instanceof && !is_wp_error() | 57 | do_action(), wp_is_password_reset_allowed_for_user(), is_wp_error(), wp_generate_password(), do_action(), time(), wp_fast_hash(), 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: 75 instructions, 11–57 executed per call, 4 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:
- do_action( retreive_password )action_deprecatedline 3025 (+15 into the body)
Fires before a new password is retrieved.
- do_action( retrieve_password )actionline 3034 (+24 into the body)
Fires before a new password is retrieved.
- do_action( retrieve_password_key )actionline 3054 (+44 into the body)
Fires when a password reset key is generated.
Uses · 9
- __()Retrieves the translation of $text.
- do_action_deprecated()Fires functions attached to a deprecated action hook.
- do_action()Calls the callback functions that have been added to an action hook.
- wp_is_password_reset_allowed_for_user()Checks if password reset is allowed for a specific user.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- wp_generate_password()Generates a random password drawn from the defined set of characters.
- wp_fast_hash()Returns a cryptographically secure hash of a message using a fast generic hash function.
- wp_update_user()Updates a user in the database.
- WP_Error::__construct()Initializes the error.
Used by · 2
- retrieve_password()Handles sending a password retrieval email to a user.
- wp_new_user_notification()Emails login credentials to a newly-registered user.
Source code
function get_password_reset_key( $user ) { 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 ); $hashed = time() . ':' . wp_fast_hash( $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.
Signature, return type and hooks compared across 5 parsed releases.
About this page
- Parsed data
- Generated from the wordpress-develop 6.8.8 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.