wp_get_current_user

Retrieve the current user object.

Description

(WP_User) wp_get_current_user(); 

Will set the current user, if the current user is not set. The current user will be set to the logged-in person. If no user is logged-in, then it will set the current user to 0, which is invalid and won't have any permissions.

Returns (WP_User)

Current WP_User instance.


Usage

  1. if ( !function_exists( 'wp_get_current_user' ) ) { 
  2. require_once ABSPATH . WPINC . '/pluggable.php'; 
  3.  
  4.  
  5. // NOTICE! Understand what this does before running. 
  6. $result = wp_get_current_user(); 
  7.  

Defined (2)

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

/wp-includes/pluggable.php  
  1. function wp_get_current_user() { 
  2. return _wp_get_current_user(); 
/wp-includes/user.php  
  1. function _wp_get_current_user() { 
  2. global $current_user; 
  3.  
  4. if ( ! empty( $current_user ) ) { 
  5. if ( $current_user instanceof WP_User ) { 
  6. return $current_user; 
  7.  
  8. // Upgrade stdClass to WP_User 
  9. if ( is_object( $current_user ) && isset( $current_user->ID ) ) { 
  10. $cur_id = $current_user->ID; 
  11. $current_user = null; 
  12. wp_set_current_user( $cur_id ); 
  13. return $current_user; 
  14.  
  15. // $current_user has a junk value. Force to WP_User with ID 0. 
  16. $current_user = null; 
  17. return $current_user; 
  18.  
  19. if ( defined('XMLRPC_REQUEST') && XMLRPC_REQUEST ) { 
  20. return $current_user; 
  21.  
  22. /** 
  23. * Filters the current user. 
  24. * The default filters use this to determine the current user from the 
  25. * request's cookies, if available. 
  26. * Returning a value of false will effectively short-circuit setting 
  27. * the current user. 
  28. * @since 3.9.0 
  29. * @param int|bool $user_id User ID if one has been determined, false otherwise. 
  30. */ 
  31. $user_id = apply_filters( 'determine_current_user', false ); 
  32. if ( ! $user_id ) { 
  33. return $current_user; 
  34.  
  35. wp_set_current_user( $user_id ); 
  36.  
  37. return $current_user;