admin管理员组

文章数量:1023758

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.

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.

本文标签: Will a plugin able to know isuserloggedin