0

When I logged into the specific multi-site, I am trying to get current login user details, I already used below one but this function returning null. How, can I get currents logged in user information. I am actually trying to get user email

$logged_in_user = wp_get_current_user();

if($logged_in_user->ID != 0){
    $email = $logged_in_user->user_email;
}
3
  • 1
    It should work. It's possible you're using it too early in the page load, before the current user has been determined. When are you running the function? – Jacob Peattie Jul 25 '17 at 12:43
  • get_currentuserinfo() , so you can use. It's important, though, to only call it on or after the init hook. Calling it before will only return 0 – Devendra Sharma Jul 25 '17 at 12:44
  • wp_get_current_user() return this object WP_User object { back_compat_keys => array(6) ( [user_firstname] => (string) first_name [user_lastname] => (string) last_name [user_description] => (string) description [user_level] => (string) wp_user_level [wp_usersettings] => (string) wp_user-settings [wp_usersettingstime] => (string) wp_user-settings-time ) data => stdClass object ID => (int) 0 caps => array(0) cap_key => null roles => array(0) allcaps => array(0) filter => null } – bharath goud Jul 25 '17 at 12:50
1

As described in the documentation, you have call this inside a hook.

For wordpress versions < 3.4: use the init or any subsequent action to call this function. Calling it outside of an action can lead to troubles. See #14024 for details.

Example:

add_action('init', 'get_user_email');
function get_user_email() {
  $logged_in_user = wp_get_current_user();

  if($logged_in_user->ID != 0){
    $email = $logged_in_user->user_email;
  }
}
2
  • I used that hook then also showing WP_User object { back_compat_keys => array(6) ( [user_firstname] => (string) first_name [user_lastname] => (string) last_name [user_description] => (string) description [user_level] => (string) wp_user_level [wp_usersettings] => (string) wp_user-settings [wp_usersettingstime] => (string) wp_user-settings-time ) data => stdClass object ID => (int) 0 caps => array(0) cap_key => null roles => array(0) allcaps => array(0) filter => null } – bharath goud Jul 25 '17 at 13:08
  • Try the global $current_user; – Cesar Henrique Damascena Jul 25 '17 at 13:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.