Wordpress get user id by login name
Ho开发者_运维知识库w do i get the user id from user login name in wordpress?
get_userdatabylogin
is now deprecated, it is possible to get the same result via
get_user_by
example :
$user = get_user_by('login','loginname');
if($user)
{
echo $user->ID;
}
link to the ticket about this change
By using get_userdatabylogin
:
$user = get_userdatabylogin('loginname');
if($user){
echo $user->ID;
}
Example:
$user = get_userdatabylogin('admin');
if($user) echo $user->ID; // Outputs 1
UPDATE: the function get_userdatabylogin
has been deprecated, and replaced with get_user_by()
. Refer to Dalen's solution to this question.
You also can use get_current_user_id()
<?php
$user_id = get_current_user_id();
echo "ID User : ".$user_id ;
if ($user_id == 0) {
echo 'You are currently not logged in.';
} else {
echo 'You are logged in as user '.$user_id;
}
?>
精彩评论