Skip directly to content

Welcome to my personal website. I am Sivaji a web developer from Chennai with 2+ years experience in Drupal.

How to set custom page title in Drupal 7

sivaji's picture
on April 10th, 2011 at 10:39:50 PM

In Drupal parlance "page title" often refers to two things. One is <HEAD> tag title that appears in browser window and the other one is <H1> tag appears in content region. Drupal offers API drupal_set_title() / drupal_get_title() to alter these together however it is often required to change <HEAD> tag title alone as it can influence site page rank. And drupal_set_title() / drupal_get_title() doesn't fits for this purpose.

Still in Drupal there is a module for thatTM , Page title, though this module is good it is really superfluous if your site is going to use same pattern like [current-page:page-title] | [site:name] | [site:slogan] across the pages. I have often encountered this requirement in the past. One quick way to achieve it without installing additional modules is theme_preprocess_html() like other theme function in Drupal it takes single argument of type array, index 'head_title' takes override title.

Below is the snippet I had to use for one of my personal projects

  1. /**
  2.  * Override or insert variables into the html template.
  3.  */
  4. function THEMENAME_preprocess_html(&$vars) {
  5.   global $theme_path;
  6.   // Add conditional CSS for IE7 and below.
  7.   drupal_add_css($theme_path . '/css/ie.css', array('group' => CSS_THEME, 'browsers' => array('IE' => 'lte IE 7', '!IE' => FALSE), 'preprocess' => FALSE));
  8.   $vars['head_title'] = implode(' | ', array(drupal_get_title(), variable_get('site_name'), variable_get('site_slogan')));  
  9. }

Add this to your default theme's template.php and replace THEMENAME with machine name of your theme. Share your Drupal tip. Happy Drupal hacking !!!

Comments

Fantastic work Sivaji, this had been driving me absolutely nuts - the changes in the template.html.php structure from D6 to D7 stopped my previous PHP snippet from working that did basically the same as this, and I was totally stumped on how to port it to the new version.

The best thing about this snippet is it works with panel pages, which is the only thing the Drupal 7 page_title module doesn't and led me to give up on with.

If (like in my case) your theme says the function has already been declared, just past in the two lines after the inline comment into the function where it has already been declared.

This feature is aimed at Microsoft Windows 8 need to securely access corporate network Windows 8 resources while away from the Office 2010 . Essentially a simple Microsoft Office 2010 replacement for VPN connections, Download Office 2010 natively optical discs Office 2010 Download and enables you to write to Office 2010 Professional recordable media. Microsoft Office 2010 Download has consolidated the most common Microsoft Office 2011 sharing tasks into a single simple interface Windows 7 .

Computers in a Microsoft Windows 7 can easily share documents, digital media files, and printers over a home network.The Win 7 contained all the features of Download Windows 7 think if you want most full functional type, Buy Windows 7 this version will be your best Office 2007 Key choice. In addition, you can save more money Office 2010 Key when you buy it.

Hi, I've been looking for any solution and forgot that D7 has for each template an separate preprocess hook. I've been cleaned the title like that:

function empty_preprocess_html(&$vars) { // blankin title $vars['head_title'] = drupal_get_title();}

Thank you for the hint anyway :].

Thanks Sivaji,

Day saver :)

Post new comment