when do we need to load user using user_load($user->uid) after global $user. Why do we need to load a user because we can access user data after declaring it global. I am adding a commerce customer to his billing and shipping profiles, should I use user_load?


The global $user object is just the bare data received from the users table for the associated user. It doesn't have any field, or other module-added data, attached to it by default.

For example, if you have a field attached to your user accounts called 'field_nickname', and you want to inspect the value of that field for the current user in code, you would need to call user_load() for the data to be available.

// Might work, but it's not guaranteed - depends on the modules 
// you have installed and what's happened in the page build so far
$account = $GLOBALS['user'];
if ($items = field_get_items('user', $account, 'field_nickname')) {
  // ...

// Will always work.
$account = user_load($GLOBALS['user']->uid);
if ($items = field_get_items('user', $account, 'field_nickname')) {
  // ...

There are situations when other modules will load the field and other module data onto the global $user object, so depending on what modules you have installed you call to user_load() might not strictly be necessary.

However, it's good practice to make sure you load the object yourself; if another module has already run it for the current user then that data is statically cached, so you don't get a performance hit.


You don't need to do user_load() on global $user.

global $user is already a loaded user (the current user).

use user_load() when you want to load users other than the current user. for example, if you have a uid for the author of a node or comment, or a uid from a commerce order, then you use user_load() to load the user object.

Unless (as Clive mentioned), you need to access fields attached to the user, in which case you should load it.

Also, if you want profile2 fields you will need to do

$profile = profile2_load_by_user($user, 'PROFILE_NAME');
  • 3
    That's not strictly true...field data isn't available by default on the global $user object for example - it needs to go through user_load() (which in turn invokes field_attach_load()) for that to happen – Clive Apr 23 '13 at 12:11
  • 1
    True, I was also going to mention things like profile2, and the fact that if you want profile2 fields they have to be separately loaded. – rooby Apr 23 '13 at 12:13

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.