admin管理员组

文章数量:1310027

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;
}

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;
}
Share Improve this question edited Jul 25, 2017 at 12:48 bravokeyl 3,3776 gold badges27 silver badges33 bronze badges asked Jul 25, 2017 at 12:40 bharath goudbharath goud 11 bronze badge 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 Commented Jul 25, 2017 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 Commented Jul 25, 2017 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 Commented Jul 25, 2017 at 12:50
Add a comment  | 

1 Answer 1

Reset to default 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;
  }
}

本文标签: plugin developmentHow to get current loggedin user details in multisite