wp_authenticate_username_password( WP_User|WP_Error|null $user, string $username, string $password ): WP_User|WP_Error
- Since
- 2.8.0
- Source
wp-includes/user.php:153
Compatibility
- WordPress
- since 2.8.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|WP_Error|null- WP_User or WP_Error object from a previous callback. Default null.
$usernamestring- Username for authentication.
$passwordstring- Password for authentication.
Return value
WP_User|WP_Error- WP_User on success, WP_Error on failure.
Performance profile
How much work a call to wp_authenticate_username_password() 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
- Heavy
- Scaling
- Constant
- Instructions
- 6–63
- Plugin surface
- 1 hook
- Called by
- 0
Reaches the database via get_user_by().
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 123.
Third-party callbacks on 'wp_authenticate_user' run inside this call, and their cost is not bounded by anything here.
Nothing in core calls this; the cost is only what you spend yourself.
What it touches
- querycontent query
get_user_by()called directly - hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize and transient. Those are the worst case, several calls deep and usually down an error path, not what a normal call pays.
What one call costs · 9 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_authenticate_username_password() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$user instanceof | 6 | none |
!($user instanceof) | 12–21 | is_wp_error() |
!($user instanceof) && !is_wp_error() | 26–28 | is_wp_error(), __(), ->add() |
!($user instanceof) && !empty($username) && !empty($password) && is_wp_error() | 26 | get_user_by(), apply_filters(), is_wp_error() |
!($user instanceof) && !empty($username) && !empty($password) | 30 | get_user_by(), __(), esc_html(), sprintf() |
!($user instanceof) && empty($username) && !is_wp_error() && empty($password) | 33–35 | is_wp_error(), __(), ->add(), __(), ->add() |
!($user instanceof) && !empty($username) && !empty($password) && !is_wp_error() && !wp_password_needs_rehash() | 46 | get_user_by(), apply_filters(), is_wp_error(), wp_check_password(), wp_password_needs_rehash() |
!($user instanceof) && !empty($username) && !empty($password) && !is_wp_error() && wp_password_needs_rehash() | 52 | get_user_by(), apply_filters(), is_wp_error(), wp_check_password(), wp_password_needs_rehash(), wp_set_password() |
!($user instanceof) && !empty($username) && !empty($password) && !is_wp_error() | 63 | get_user_by(), apply_filters(), is_wp_error(), wp_check_password(), __(), esc_html(), sprintf(), wp_lostpassword_url(), __() |
Across PHP versions
Compiles the same on PHP 7.4, 8.1, 8.2, 8.3, 8.4, 8.5 and 8.6-dev: 123 instructions, 6–63 executed per call, 10 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 · 1
One hook fires while wp_authenticate_username_password() runs, in this order:
- apply_filters( wp_authenticate_user )filterline 203 (+50 into the body)
Filters whether the given user can be authenticated with the provided password.
Uses · 10
- is_wp_error()Checks whether the given variable is a WordPress Error.
- __()Retrieves the translation of $text.
- get_user_by()Retrieves user info by a given field.
- esc_html()Escaping for HTML blocks.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- wp_check_password()Checks a plaintext password against a hashed password.
- wp_lostpassword_url()Returns the URL that allows the user to reset the lost password.
- wp_password_needs_rehash()Checks whether a password hash needs to be rehashed.
- wp_set_password()Updates the user's password with a new hashed one.
- WP_Error::__construct()Initializes the error.
Source code
function wp_authenticate_username_password( $user, $username, #[\SensitiveParameter] $password) { if ( $user instanceof WP_User ) { return $user; } if ( empty( $username ) || empty( $password ) ) { if ( is_wp_error( $user ) ) { return $user; } $error = new WP_Error(); if ( empty( $username ) ) { $error->add( 'empty_username', __( '<strong>Error:</strong> The username field is empty.' ) ); } if ( empty( $password ) ) { $error->add( 'empty_password', __( '<strong>Error:</strong> The password field is empty.' ) ); } return $error; } $user = get_user_by( 'login', $username ); if ( ! $user ) { return new WP_Error( 'invalid_username', sprintf( /* translators: %s: User name. */ __( '<strong>Error:</strong> The username <strong>%s</strong> is not registered on this site. If you are unsure of your username, try your email address instead.' ), esc_html( $username ) ) ); } /** * Filters whether the given user can be authenticated with the provided password. * * @since 2.5.0 * * @param WP_User|WP_Error $user WP_User or WP_Error object if a previous * callback failed authentication. * @param string $password Password to check against the user. */ $user = apply_filters( 'wp_authenticate_user', $user, $password ); if ( is_wp_error( $user ) ) { return $user; } $valid = wp_check_password( $password, $user->user_pass, $user->ID ); if ( ! $valid ) { return new WP_Error( 'incorrect_password', sprintf( /* translators: %s: User name. */ __( '<strong>Error:</strong> The password you entered for the username %s is incorrect.' ), '<strong>' . esc_html( $username ) . '</strong>' ) . ' <a href="' . wp_lostpassword_url() . '">' . __( 'Lost your password?' ) . '</a>' ); } if ( wp_password_needs_rehash( $user->user_pass, $user->ID ) ) { wp_set_password( $password, $user->ID ); } return $user;}Changelog
Introduced in 2.8.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 7.1.0 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.