最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Will a plugin able to know is_user_logged_in?

matteradmin8PV0评论

I am currently building a plugin and it doesn't know when a user is logged in.

global $current_user;
echo $current_user->ID;

It works when embedded on a template but not on a plugin? I have a custom database that depends on user_id, whenever I insert a record, user_id field always has a zero value while others have the correct one.

I am currently building a plugin and it doesn't know when a user is logged in.

global $current_user;
echo $current_user->ID;

It works when embedded on a template but not on a plugin? I have a custom database that depends on user_id, whenever I insert a record, user_id field always has a zero value while others have the correct one.

Share Improve this question asked Nov 29, 2010 at 15:14 JoannJoann 1,4853 gold badges18 silver badges22 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 1

The $current_user global isn't setup until right before the 'init' action is called. So any code using it shouldn't be fired until that action or later.

I would also suggest using the get_current_user_id() method instead of getting the global directly.

Try using is_user_logged_in() to check whether the user is logged in. It will return true or false depending on whether the user is logged in.

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far