Customizing how your page headers are displayed for search engines

Last modified: June 20, 2009 - 18:13

Background

The default way your page title headers appear in browser window's title bar and subsequently in search engine results like google.com is as follows:

Drupals default page header format
**********************************

(Site front page) SITE NAME | SITE SLOGAN

(All other pages) NODE TITLE | SITE NAME

This is widely regarded as the best approach, as people reading English read from left-to-right and are usually using a search engine to search for a topic, rather than a specific site. Therefore, it's arguably more ergonomic to leave the Drupal default.

Changing how your page titles are constructed

Drupal's flexibility allows you to change the way your page titles are constructed to whatever you want and dynamically, based on what page or section your content is in.

Step 1 of 1

The following only applies to Drupal sites using phptemplate based themes.

  1. Open your page.tpl.php file in a text editor like notepad.exe or equivalent.
  2. At the very top of the file, you should see the following line

    <title>
    <?php print $head_title; ?>
    </title>

  3. Change it to this:

    <title>
    <?php
    print "$node->title | $site_name | $site_slogan";
    ?>

    </title>

  4. As the variable names suggests, that re-orders your page tite to read: NODE TITLE | SITE NAME | SITE SLOGAN
  5. Upload your edited page.tpl.php file to your active theme folder for the changes to take effect.

Available variables

The following is a list of relevant variable names that are available to the page.tpl.php file by default.

For a full list of all the variables available, visit the page.tpl.php overview page in the phptemplate section of the handbook.

  • site: The name of the site, always filled in.
  • site_name: The site name of the site, to be used in the header, empty when display has been disabled.
  • site_slogan: The slogan of the site, empty when display has been disabled.
  • title: Title, different from head_title, as this is just the node title most of the time.
  • breadcrumb: HTML for displaying the breadcrumbs at the top of the page.
  • mission: The text of the site mission.
  • is_front: True if the front page is currently being displayed. Used to toggle the mission.
  • footer_message: The footer message as defined in the admin settings.

My change puts SITE NAME first

liza - February 26, 2006 - 05:47

This change is made in the PHPTemplate engine itself. All themes should automatically be updated :

$vars = array(
    'head_title'          =>  variable_get('site_name', 'drupal') .' | '.  (drupal_get_title() ? strip_tags(drupal_get_title()) : variable_get('site_slogan', '')),

My follow up here.

Variable for site mission

tela - March 13, 2007 - 10:25

In the list of available variables, to display the site mission, use site_mission (mission doesn't work).

And here is the code I added to template.php to change my page titles (adapted from liza code):

<?php
$vars
= array('head_title' =>  variable_get('site_name', 'drupal') .' | '.  (drupal_get_title() ? strip_tags(drupal_get_title()) : variable_get('site_mission', '')));
?>

Output:

  • For the frontpage: Site name | Site mission
  • For the other pages: Site name | Page title

This works with Drupal 5.0.

SEO

tjholowaychuk - December 3, 2007 - 19:07

Arguably the pipe characters "|" should not even be in the title tag at all, as well as the site name should not be repeated for specific pages that are not about your site name they are about a topic. Again this is all arguable but from my experience with search engine optimization this works best.
____________________________________________________
Tj Holowaychuk

Vision Media
350designs

Page_title module

Summit - December 5, 2007 - 10:43

Hi,

You could use the page title module for drupal 5! and set the variables there, with or without the |.
Agreed that the | is not benefitially related to SEO.

greetings,
Martijn

Please add a link on
http://www.gratis-informatie.nl/link-ruil-aanvraag
off course I will link back to you also!
http://www.isnow.in is my new project.

Replace title pipe with a hyphen

DerekAhmedzai - January 7, 2008 - 10:47

Alternatively, you can change this in your theme.
Open up the template.php file and do a quick search and replace on the pipe (|) character.

This example uses a hypen :-

<?php
function _phptemplate_variables($hook, $vars = array()) {
  if (
$hook == 'page') {
   
//replace pipe in page title with hyphen
   
$vars['head_title'] =  str_replace(" | ", " - ", $vars['head_title']);
  }
  return
$vars;
}
?>

pipe is out

Jacob - January 20, 2008 - 14:03

I'm in - pipe should be removed by default at least.
Meantime the remedy can be found here: http://drupal.org/node/103954

Jacob.

 
 

Drupal is a registered trademark of Dries Buytaert.