set_current_user

Changes the current user by ID or name.

Description

(WP_User) set_current_user( (int|null) $id, (string) $name = '' ); 

Set $id to null and specify a name if you do not know a user's ID.

Returns (WP_User)

returns wp_set_current_user()

Parameters (2)

0. $id (int|null)
User ID.
1. $name — Optional. (string) => ''
The user's username

Usage

  1. if ( !function_exists( 'set_current_user' ) ) { 
  2. require_once ABSPATH . WPINC . '/pluggable-deprecated.php'; 
  3.  
  4. // User ID. 
  5. $id = null; 
  6.  
  7. // Optional. The user's username 
  8. $name = ''; 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = set_current_user($id, $name); 
  12.  

Defined (1)

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

/wp-includes/pluggable-deprecated.php  
  1. function set_current_user($id, $name = '') { 
  2. _deprecated_function( __FUNCTION__, '3.0.0', 'wp_set_current_user()' ); 
  3. return wp_set_current_user($id, $name);