How can I get the user ID and load the user with user_load() using the $user_profile variable in the user-profile.tpl.php template file?

3 Answers 3


Using the following code in a module will make the user ID available to the user-profile.tpl.php template as $user_id.

function mymodule_preprocess_user_profile(&$variables) {
  $account = $variables['elements']['#account'];
  // Add the user ID into the user profile as a variable
  $variables['user_id'] = $account->uid;
  // Helpful $user_profile variable for templates.
  foreach (element_children($variables['elements']) as $key) {
    $variables['user_profile'][$key] = $variables['elements'][$key];

  // Preprocess fields.
  field_attach_preprocess('user', $account, $variables['elements'], $variables);

This means that you can then copy the user-profile.tpl.php template from the modules/user directory to your theme directory and change it to the following.

<div class="profile"<?php print $attributes; ?>>
  <?php print $user_id; ?>
  <?php print render($user_profile); ?>

You cannot get the uid from $user_profile as it does not contain any information we can use to get the current user.

To get the current user's object, use the following:

global $user;
echo $user->uid;
  • Thank you but I wanted the requested user not the current. Anyway, I've find another way to do
    – Splinteer
    Feb 29, 2016 at 15:04

The user-profile.tpl.php template file doesn't get a variable containing the user ID, nor a user object. The $user_profile variable contains what needs to be rendered for the user profile. template_preprocess_user_profile() retrieves the user object used for the profile, but it doesn't pass it to the template file, nor does it pass the user ID.

If the user ID is necessary in the user-profile.tpl.php template file, implementing hook_preprocess_HOOK() helps.

function mymodule_preprocess_user_profile(&$variables) {
  if (isset($variables['elements']['#account']) && isset($variables['elements']['#account']->uid)) {
    $variables['uid'] = $variables['elements']['#account']->uid;

The template file can then use $uid to get the user ID.

Notice that copying other code from template_preprocess_user_profile() is not necessary: template_preprocess_user_profile() is executed before any preprocess and process functions, whenever they are implemented in modules or in themes. An implementation of hook_preprocess_user_profile() doesn't override/replace template_preprocess_user_profile().

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.