Authenticates a user using the email and password.
Parameters
Source
function wp_authenticate_email_password( $user, $email, #[\SensitiveParameter] $password ) { if ( $user instanceof WP_User ) { return $user; } if ( empty( $email ) || empty( $password ) ) { if ( is_wp_error( $user ) ) { return $user; } $error = new WP_Error(); if ( empty( $email ) ) { // Uses 'empty_username' for back-compat with wp_signon(). $error->add( 'empty_username', __( '<strong>Error:</strong> The email field is empty.' ) ); } if ( empty( $password ) ) { $error->add( 'empty_password', __( '<strong>Error:</strong> The password field is empty.' ) ); } return $error; } if ( ! is_email( $email ) ) { return $user; } $user = get_user_by( 'email', $email ); if ( ! $user ) { return new WP_Error( 'invalid_email', __( 'Unknown email address. Check again or try your username.' ) ); } /** This filter is documented in wp-includes/user.php */ $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: Email address. */ __( '<strong>Error:</strong> The password you entered for the email address %s is incorrect.' ), '<strong>' . $email . '</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; } Hooks
- apply_filters( ‘wp_authenticate_user’,
WP_User|WP_Error $user ,string $password ) Filters whether the given user can be authenticated with the provided password.
Changelog
| Version | Description |
|---|---|
| 4.5.0 | Introduced. |
If the email parameter is not a valid email address (e.g. does not contain an @ sign), wp_authenticate_email_password will not return WP_Error; it will return NULL
wp_authenticate_email_password()will return whatever was passed in the$userparameter: ifNULLwas passed, thenNULLwill be returned.