I would like to print something in page-user.tpl.php only when the current user is accessing his own user page. (any URL starting with /user/%uid/ but when %uid/ is the user id of the current logged in user)

For example, printing something like "Your are viewing your own user page now." in page-user.tpl.php

How can I make this condtion?


There are two paths a user could be visiting their own page: "user" and "user/UID" where their UID matches.

There is a global variable $user that stores the currently logged in user.

So in the variable preprocessor for the template you can determine whether or not a user is on their own user page and save it to a variable that you can reference in the template file, like below.
Note the use of $variables['logged_in'], which is an available variable as listed in the page.tpl.php documentation.

 * Override or insert variables into the page templates.
 * @param $variables
 *   An array of variables to pass to the theme template.
function THEME_NAME_preprocess_page(&$variables) {
  // If we are on the 'user' page or a sub-page of it.
  if (arg(0) == 'user') {
    $variables['own_account'] = FALSE;
    // If we are on 'user' and are logged in then we are on our own user page.
    if (!arg(1) && $variables['logged_in']) {
      $variables['own_account'] = TRUE;
    // If we are on 'user/UID' then check against the current user.
    else if (is_numeric(arg(1)) && !arg(2)) {
      global $user;
      if ($user->uid == arg(1)) {
        $variables['own_account'] = TRUE;

Then you can use the variable $own_profile in your page-user.tpl.php template, which in this case might look something like this:

<?php if ($own_account): ?>
  <div class="own-account-msg">
    Here is some markup for the message a user sees when they are on their own account page.
<?php endif; ?>
  • Thank you. It works! Only one thing is that it works only on "user" and "user/UID" (I think that's what you have mentioned about the user page.) But is there anyway to make it to work also on "user/UID/edit" or other custom view pages like "user/UID/my_custom_view_page" ? I mean, all the pages that page-user.tpl.php is applied to. – Sona7 Feb 11 '16 at 14:51
  • Yep, you can remove the " && !arg(2)" part from the else if to get all sub pages of the user account. – rooby Feb 11 '16 at 22:46

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.