I am developing a website in WordPress and came across with an issue, I am unable to get current user/logged user id in WordPress plugin. below code is working while I am on a page.

$userID = get_current_user_id();

Is something wrong with the above method while using it within the plugin?

  • 2
    An important note, but when and where are you calling the function? – Tom J Nowell Mar 19 '18 at 10:22
  • 2
    Also, WordPress.com is a 3rd party service, are you sure you meant to tag wp.com? – Tom J Nowell Mar 19 '18 at 10:22

You probably called it too early, get_current_user_id uses wp_get_current_user, so move all your code that's at the top level of functions.php into the init hook or appropriate equivalent. The only thing that should be in the global scope in your functions file if you want maximum compatability and efficiency is add_action and add_filter calls

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.