Customizing the Title on Account Pages in Drupal 7 Using a Theme Preprocess Function


The Drupal 7 Account pages (user, user/register, user/password), all have the same Title, i.e. "User Account". I just ran up against this issue and my client wanted different / custom titles on these pages. It turns out there is an issue filed against core. For now the issue is for Drupal 8 and will probably be backported for Drupal 7 eventually once the fix for Drupal 8 is committed.

Custom title shown for user account pages

Workaround Solution

In the meantime, I searched around drupal.org for a fix and ended up with a nice template preprocess function that was implemented for Adaptive Theme. It seems pretty elegant and is very flexible in that you can customize your own titles for these pages in your own theme. 

The Code

In your theme's template.php file, add this code as a page preprocess function. Note that "my_theme" should be the machine name of your theme. I have also accounted for the fact that user and user/login are two urls but actually the same page. I have also added a check to see whether or no the user is logged in or not for added flexibility. 

function my_theme_preprocess_page(&$variables) {
  // check if the user is logged in.
  global $user;
  if ($user->uid) {
    // Logged in user.
  }
  else {
    // Not logged in.
    // if not logged in then give generic custom titles.
    //allow customization of user / login / password page titles.

    if (arg(0) == 'user' && arg(1) == 'login') {
      drupal_set_title(t('Login'));
    }

    if (arg(0) == 'user') {
      drupal_set_title(t('Login'));
    }

    if (arg(0) == 'user' && arg(1) == 'password') {
      drupal_set_title(t('Request password'));
    }

    if (arg(0) == 'user' && arg(1) == 'register') {
      drupal_set_title(t('Create new account'));
    }
  }
}

Resources

Tags