wp_authenticate( string $username, string $password ): WP_User|WP_Error
- Since
- 2.5.0, 4.5.0
- Source
wp-includes/pluggable.php:684
Compatibility
- WordPress
- since 4.5.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
$usernamestring- User's username or email address.
$passwordstring- User's password.
Return value
WP_User|WP_Error- WP_User object if the credentials are valid, otherwise WP_Error.
Performance profile
How much work a call to wp_authenticate() 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
- Trivial
- Scaling
- Constant
- Instructions
- 24–42
- Plugin surface
- 2 hooks
- Called by
- 4
Touches nothing outside its own arguments.
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 42.
Third-party callbacks on 'authenticate', 'wp_login_failed' run inside this call, and their cost is not bounded by anything here.
4 places in core call this, so the cost is paid more often than your own code shows.
What it touches
- hookthird-party callbacks
apply_filters()called directly
Further down the call graph this can also reach option, cache, serialize, query 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 · 6 distinct outcomes
One number would be a lie: the work depends on which branch runs. These are every distinct cost wp_authenticate() can have, taken from its control-flow graph on PHP 8.5.
| When | Instructions | Calls it makes |
|---|---|---|
$user !== null && $user !== false && !is_wp_error() | 24 | sanitize_user(), apply_filters(), is_wp_error() |
$user !== null && $user !== false && is_wp_error() | 28 | sanitize_user(), apply_filters(), is_wp_error(), ->get_error_code() |
!is_wp_error() | 30–32 | sanitize_user(), apply_filters(), __(), is_wp_error() |
is_wp_error() | 34–36 | sanitize_user(), apply_filters(), __(), is_wp_error(), ->get_error_code() |
$user !== null && $user !== false && is_wp_error() | 34 | sanitize_user(), apply_filters(), is_wp_error(), ->get_error_code(), do_action() |
is_wp_error() | 40–42 | sanitize_user(), apply_filters(), __(), is_wp_error(), ->get_error_code(), do_action() |
Across PHP versions
| PHP | Compiled | Executed | Branches | Notes |
|---|---|---|---|---|
| 8.6-dev | 42 | 24–42 | 4 | |
| 8.5 | 42 | 24–42 | 4 | |
| 8.4 | 42 | 24–42 | 4 | 2 fewer instructions than PHP 8.3 |
| 8.3 | 44 | 26–44 | 4 | |
| 8.2 | 44 | 26–44 | 4 | |
| 8.1 | 44 | 26–44 | 4 | |
| 7.4 | 44 | 26–44 | 4 |
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 · 2
2 hooks fire while wp_authenticate() runs, in this order:
- apply_filters( authenticate )filterline 706 (+22 into the body)
Filters whether a set of user login credentials are valid.
Uses · 6
- sanitize_user()Sanitizes a username, stripping out unsafe characters.
- apply_filters()Calls the callback functions that have been added to a filter hook.
- __()Retrieves the translation of $text.
- is_wp_error()Checks whether the given variable is a WordPress Error.
- do_action()Calls the callback functions that have been added to an action hook.
- WP_Error::__construct()Initializes the error.
Used by · 4
- user_pass_ok()Check that the user login name and password is correct.
- wp_login()Checks a users login information and logs them in if it checks out. This function is deprecated.
- wp_signon()Authenticates and logs a user in with 'remember' capability.
- wp_xmlrpc_server::login()Logs user in.
Source code
function wp_authenticate( $username, #[\SensitiveParameter] $password ) { $username = sanitize_user( $username ); $password = trim( $password ); /** * Filters whether a set of user login credentials are valid. * * A WP_User object is returned if the credentials authenticate a user. * WP_Error or null otherwise. * * @since 2.8.0 * @since 4.5.0 `$username` now accepts an email address. * * @param null|WP_User|WP_Error $user WP_User if the user is authenticated. * WP_Error or null otherwise. * @param string $username Username or email address. * @param string $password User password. */ $user = apply_filters( 'authenticate', null, $username, $password ); if ( null === $user || false === $user ) { /* * TODO: What should the error message be? (Or would these even happen?) * Only needed if all authentication handlers fail to return anything. */ $user = new WP_Error( 'authentication_failed', __( '<strong>Error:</strong> Invalid username, email address or incorrect password.' ) ); } $ignore_codes = array( 'empty_username', 'empty_password' ); if ( is_wp_error( $user ) && ! in_array( $user->get_error_code(), $ignore_codes, true ) ) { $error = $user; /** * Fires after a user login has failed. * * @since 2.5.0 * @since 4.5.0 The value of `$username` can now be an email address. * @since 5.4.0 The `$error` parameter was added. * * @param string $username Username or email address. * @param WP_Error $error A WP_Error object with the authentication failure details. */ do_action( 'wp_login_failed', $username, $error ); } return $user; }Changelog
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
$username now accepts an email address.from the docblockAbout this page
- Parsed data
- Generated from the wordpress-develop 7.1.0 tag, from
src/wp-includes/pluggable.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.