At what point during the Wordpress Lifecycle can I use wp_get_current_user?
I'm writing an integration pie开发者_如何学运维ce with a third party system and need to check certain values when a user logs in.
I've tried several different hooks: wp_init, wp_login and wp_loaded, but wp_get_current_user always returns null.
E.g.: To determine if there is a user currently logged in, do this:
<?php
wp_get_current_user();
if ( 0 == $current_user->ID ) {
// Not logged in.
} else {
// Logged in.
}
?>
default usage The call to wp_get_current_user() return WP_User object.
<?php
wp_get_current_user();
/**
* @example Safe usage: $current_user = wp_get_current_user();
* if ( !($current_user instanceof WP_User) )
* return;
*/
echo 'Username: ' . $current_user->user_login . '<br />';
echo 'User email: ' . $current_user->user_email . '<br />';
echo 'User level: ' . $current_user->user_level . '<br />';
echo 'User first name: ' . $current_user->user_firstname . '<br />';
echo 'User last name: ' . $current_user->user_lastname . '<br />';
echo 'User display name: ' . $current_user->display_name . '<br />';
echo 'User ID: ' . $current_user->ID . '<br />';
?>
more on this page http://codex.wordpress.org/Function_Reference/wp_get_current_user
I was able to accomplish this using http://codex.wordpress.org/Function_Reference/get_user_by.
function CheckWordPressLogin($User) {
//$User contains login user name.
$CurrentUser = get_user_by('login', $User); //$CurrentUser is a WP_User object.
}
add_action('wp_login', 'CheckWordPressLogin');
精彩评论