Unable to use user fields from the global variable $user I have created a module where I am declaring $user as global variable in hook_init().  I can access the values of $user-&gt;x_field in page.tpl.php. When I try to use $user-&gt;x_field in hook_contents($display), it causes the following error:


  Undefined variable:x_field


Can I use the user fields in hook_contents()? Am I doing it the wrong way?
The global $user variable doesn't contain all the values you would find from a user object loaded from the database; in particular, it doesn't contain any field associated with the user entity. If you need a full-loaded user object for the currently logged-in user, you should use the following code.

$account = user_uid_optional_load();
// Use $account to access the user fields.


If need to load a full-loaded user object knowing its user ID, then you should use the following code.

$account = user_load($1);
// Use $account to access the user fields.


Also in this case, you could use user_uid_optional_load(); since it only passes the given user ID to user_load(), you can directly use user_load() and use user_uid_optional_load() when you don't have always a user ID, and need the user object for the currently logged-in user in that case.