Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

Thanks, William

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

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()?

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.

BTW: leaving your name in the ending seems to violate the rule.

share|improve this answer
2  
I know doing homework and reading manuals sucks, but the pages faq, about, How to Ask and How to Answer are really helpful to get the best of the Stack. Rant aside, very good Question and Answer, kudos for that, +1 to both! –  brasofilo Mar 9 '13 at 14:36
    
Awesome answer! –  William Mar 9 '13 at 18:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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