wp_authenticate_cookie() WordPress Function

The wp_authenticate_cookie() function is used to authenticate a user using their cookie. This function is used when a user logs in to WordPress.

wp_authenticate_cookie( WP_User|WP_Error|null $user, string $username, string $password ) #

Authenticates the user using the WordPress auth cookie.


Parameters

$user

(WP_User|WP_Error|null)(Required)WP_User or WP_Error object from a previous callback. Default null.

$username

(string)(Required)Username. If not empty, cancels the cookie authentication.

$password

(string)(Required)Password. If not empty, cancels the cookie authentication.


Top ↑

Return

(WP_User|WP_Error) WP_User on success, WP_Error on failure.


Top ↑

Source

File: wp-includes/user.php

function wp_authenticate_cookie( $user, $username, $password ) {
	if ( $user instanceof WP_User ) {
		return $user;
	}

	if ( empty( $username ) && empty( $password ) ) {
		$user_id = wp_validate_auth_cookie();
		if ( $user_id ) {
			return new WP_User( $user_id );
		}

		global $auth_secure_cookie;

		if ( $auth_secure_cookie ) {
			$auth_cookie = SECURE_AUTH_COOKIE;
		} else {
			$auth_cookie = AUTH_COOKIE;
		}

		if ( ! empty( $_COOKIE[ $auth_cookie ] ) ) {
			return new WP_Error( 'expired_session', __( 'Please log in again.' ) );
		}

		// If the cookie is not set, be silent.
	}

	return $user;
}


Top ↑

Changelog

Changelog
VersionDescription
2.8.0Introduced.

The content displayed on this page has been created in part by processing WordPress source code files which are made available under the GPLv2 (or a later version) license by theĀ Free Software Foundation. In addition to this, the content includes user-written examples and information. All material is subject to review and curation by the WPPaste.com community.

Show More
Show More