authenticatefilter-hookWP 2.8.0

Allows you to perform additional checks on a user's authentication credentials (login/password) before the user is authenticated.

The filter fires whenever a user logs in to the site.

This filter performs WordPress's basic login/password check.

Use the wp_authenticate_user hook when additional authentication checks must run after WordPress's basic check but before the user is authenticated.

Usage

add_filter( 'authenticate', 'wp_kama_authenticate_filter', 10, 3 );

/**
 * Function for `authenticate` filter-hook.
 * 
 * @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.
 *
 * @return null|WP_User|WP_Error
 */
function wp_kama_authenticate_filter( $user, $username, $password ){

	// filter...
	return $user;
}
$user(null/WP_User/WP_Error)

A WP_User object if the user passed the check, a WP_Error object, or null.

The callback can return:

  • WP_User — the check passed, and subsequent checks may proceed.
  • null — no authentication check has occurred yet.
  • WP_Error — a check occurred, but the credentials failed it.
$username(string)
Username or email address. Email addresses are supported since WP 4.5.0; before that, only a username could be specified.
$password(string)
Password in unencrypted form.

Examples

#1 Cookie authentication

After you enter a username and password and are authenticated, a temporary hash of the password is stored in a cookie, functioning somewhat like a session.

Authentication from this cookie hash also occurs on this hook through wp_authenticate_cookie().

add_filter( 'authenticate', 'wp_authenticate_cookie', 30, 3 );

#2 Application Password authentication

After Application Passwords were added, authentication with an Application Password also began to occur on this hook. It is handled by wp_authenticate_application_password().

add_filter( 'authenticate', 'wp_authenticate_application_password', 20, 3 );

#3 Basic WordPress authentication check (username and password)

Three functions are attached to the authenticate hook for the basic check:

From /wp-includes/default-filters.php.

// Default authentication filters.
add_filter( 'authenticate', 'wp_authenticate_username_password',  20, 3 );
add_filter( 'authenticate', 'wp_authenticate_email_password',     20, 3 );
add_filter( 'authenticate', 'wp_authenticate_spam_check',         99    );

Changelog

Since 2.8.0 Introduced.
Since 4.5.0 $username now accepts an email address.

Where the hook is called

wp_authenticate()
authenticate
wp-includes/pluggable.php 706
$user = apply_filters( 'authenticate', null, $username, $password );

Where the hook is used in WordPress

wp-includes/default-filters.php 516
add_filter( 'authenticate', 'wp_authenticate_username_password', 20, 3 );
wp-includes/default-filters.php 517
add_filter( 'authenticate', 'wp_authenticate_email_password', 20, 3 );
wp-includes/default-filters.php 518
add_filter( 'authenticate', 'wp_authenticate_application_password', 20, 3 );
wp-includes/default-filters.php 519
add_filter( 'authenticate', 'wp_authenticate_spam_check', 99 );
wp-includes/user.php 107
add_filter( 'authenticate', 'wp_authenticate_cookie', 30, 3 );