admin管理员组文章数量:1404357
I want to get the ID of the currently logged in user. I've found the function:
get_current_user_id
which seems to be working, however I'm not sure what 'current' means in this context.
Meaning if the user views the profile of another user, then will the current user change to become the other user?
In short I'm looking for a reliable way to always get only the id of the logged in user.
I want to get the ID of the currently logged in user. I've found the function:
get_current_user_id
which seems to be working, however I'm not sure what 'current' means in this context.
Meaning if the user views the profile of another user, then will the current user change to become the other user?
In short I'm looking for a reliable way to always get only the id of the logged in user.
Share Improve this question asked Dec 27, 2012 at 19:17 Click UpvoteClick Upvote 2231 silver badge8 bronze badges2 Answers
Reset to default 2get_current_user_id()
effectively does what @Giri had described in the first snippet. The internal Wordpress function-call chain eventually calls get_currentuserinfo()
which already checks if there is a WP_User object, meaning a user is logged in.
Thus, from what I can see in the linked code, get_current_user_id()
always returns the ID of the user that is logged in or zero 0
.
In the current default theme twentytwelve
they are using get_the_author_meta()
to retrieve information about the user, for whom the current author page is displayed. So the difference in WP terminology seems to be "user" for th current, logged in user and "author" for some user, identified by an ID. See: twentytwelve/author.php.
if( is_user_logged_in() ) {
global $current_user;
get_currentuserinfo();
$userid = $current_user->ID;
}
or
Use it like this
if( is_user_logged_in() ) {
$userid = get_current_user_id();
}
- is_user_logged_in()
- get_currentuserinfo()
- get_current_user_id()
本文标签: plugin developmentHow to get the ID of the currently logged in user
版权声明:本文标题:plugin development - How to get the ID of the currently logged in user? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744788353a2625137.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论