2

I am trying to use information about the current user in a plugin that I am designing and I have seen people go about it in several different ways.

My Way

This way seems to work, but am I missing something?

global $current_user;

// Use information
echo "User ID: " . $current_user->user_id;
echo "User First Name: " . $current_user->first_name;

I have seen some people call the function get_currentuserinfo() on the next line after declaring the global variable $current_user. However, this seems to work without that call - so is it necessary?

Also, there is the function wp_get_current_user() - what is the difference between this and get_currentuserinfo()?

I have also seen people use a global variable called $profileuser and use get_user_to_edit() in order to set it to the user object. Is there some benefit to this?

I have also seen people refer directly to the $user_ID global variable in addition to using the $current_user. Why wouldn't they just use $current_user->ID?

8
  1. Call the function get_currentuserinfo() on the next line after declaring the global variable $current_user

  2. What is the difference between wp_get_current_user() and get_currentuserinfo()?

Below is a snippet:

function wp_get_current_user() {

    global $current_user;

    get_currentuserinfo();

    return $current_user;
}

I think the source code answers your first two questions, right?

Remember that wp_get_current_user() is defined in wp-includes/pluggable.php so it can be overridden.

Also, it is safe to stick with global $current_user this is because WordPress calls wp_get_current_user() during initialization.

Specifically, wp-settings.php -> new WP -> WP->init() -> wp_get_current_user


The usage of $profileuser, this global variable is only available when you are editing a user (user-edit.php) and the data will be thereof.

The last one questions I don't really know how to answer so I'll leave to others.

Hope you don't mind.

1
  • FYI: get_currentuserinfo() is deprecated as at WordPress 4.5.0. Use wp_get_current_user() instead. Jun 5 '17 at 15:16

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.