wp_authenticate_cookie

Authenticate the user using the WordPress auth cookie.

Description

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

Returns (WP_User|WP_Error)

WP_User on success, WP_Error on failure.

Parameters (3)

0. $user (WP_User|WP_Error|null)
WP_User or WP_Error object from a previous callback. Default null.
1. $username (string)
Username. If not empty, cancels the cookie authentication.
2. $password (string)
Password. If not empty, cancels the cookie authentication.

Usage

  1. if ( !function_exists( 'wp_authenticate_cookie' ) ) { 
  2. require_once ABSPATH . WPINC . '/user.php'; 
  3.  
  4. // WP_User or WP_Error object from a previous callback. Default null. 
  5. $user = null; 
  6.  
  7. // Username. If not empty, cancels the cookie authentication. 
  8. $username = ''; 
  9.  
  10. // Password. If not empty, cancels the cookie authentication. 
  11. $password = ''; 
  12.  
  13. // NOTICE! Understand what this does before running. 
  14. $result = wp_authenticate_cookie($user, $username, $password); 
  15.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/user.php  
  1. function wp_authenticate_cookie($user, $username, $password) { 
  2. if ( $user instanceof WP_User ) { 
  3. return $user; 
  4.  
  5. if ( empty($username) && empty($password) ) { 
  6. $user_id = wp_validate_auth_cookie(); 
  7. if ( $user_id ) 
  8. return new WP_User($user_id); 
  9.  
  10. global $auth_secure_cookie; 
  11.  
  12. if ( $auth_secure_cookie ) 
  13. $auth_cookie = SECURE_AUTH_COOKIE
  14. else 
  15. $auth_cookie = AUTH_COOKIE
  16.  
  17. if ( !empty($_COOKIE[$auth_cookie]) ) 
  18. return new WP_Error('expired_session', __('Please log in again.')); 
  19.  
  20. // If the cookie is not set, be silent. 
  21.  
  22. return $user;