wp_authenticate( string $username, string $password ): WP_User|WP_Error
- Since
- 2.5.0, 4.5.0
- Source
wp-includes/pluggable.php:684
Authenticates a user, confirming the login credentials are valid.
Parameters
$usernamestring- User's username or email address.
$passwordstring- User's password.
Return
WP_User|WP_Error- WP_User object if the credentials are valid, otherwise WP_Error.
Hooks 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
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; }History
Introduced in 2.5.0. Unchanged from 6.7.7 through 7.1.0.
Signature, return type and hooks compared across 5 parsed releases.
4.5.0
$username now accepts an email address.from the docblock2.5.0
Introduced.from the docblock
About 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.