I need to get logged in user id inside my plugin.

Is this the proper way?

$root = dirname(dirname(dirname(dirname(__FILE__))));
if (file_exists($root.'/wp-load.php')) {
$user_id = get_current_user_id();

Otto saying that we shouldn't load wp-load.php file since we have no idea where wp-load.php file located and it doubles the server load.

So I'm really confused.

And one more question..

Check this Rarst's Image

According to that image wp-load.php loaded before all the plugins. So why all plugin authors include that file again?


2 Answers 2


If you're inside a plugin, WordPress is already loaded. You don't need to load it yourself.

Inside whatever function needs the user ID, you need to do two things:

  1. Globalize the user data variable
  2. Populate the user data variable

Here's some pseudocode:

function some_function_that_needs_user_info() {
    global $current_user;

    // Now reference $current_user->ID in your code.
  • 2
    FYI: get_currentuserinfo() is deprecated as at WordPress 4.5.0. Use wp_get_current_user() instead. ... more information here. Commented Jun 7, 2017 at 8:14

Use is_user_logged_in() and wp_get_current_user() (dev docs).

if ( is_user_logged_in() ) {
    // Current user is logged in,
    // so let's get current user info
    $current_user = wp_get_current_user();
    // User ID
    $user_id = $current_user->ID;
  • Thanks. So this plugin coded in the wrong way right? (line 12 - 17) Commented Nov 28, 2012 at 17:27
  • That's really a separate question. Without digging more into that Plugin, I can't really comment on why it's loading wp-load.php. I do know, however, that it is not necessary for getting current user info. Commented Nov 28, 2012 at 17:29
  • It's actually necessary for that purpose, due to the fact that it's loading the majority of the WordPress core. My assumption is that this is a part of the plugin that is called outside of the scope of WordPress - such as a remote AJAX call to the file or anything related to that, where you have no access to the WordPress functions such as get_currentuserinfo. However, it's not really a good practice to include the core this way. Commented Nov 28, 2012 at 17:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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