How to customize the title of the front page programmatically in Drupal 7

This is the article number 9 of the series Practical Drupal Development. In this article, I'm going to show you how to customize the title of the front page in Drupal 7 by using preprocess functions.

The Goal

One of your clients is unhappy with how the information related to his site is shown by Search Engines.

You are asked for a new title and description, so that they look like this:

Because he likes how the search results for all the pages (with the exception of the front page) are shown, you just need to customize the title and description for the front page.

Preprocess functions

When a piece of code passes data to the theme() function for a particular theme hook, you can alter that data by using preprocess functions.

Since you will define the preprocess function for the html theme hook in the template.php file of your main theme, the preprocess function looks like this:

function [theme]_preprocess_[theme hook name](&$variables) {
}

Replace [theme] with the the name of your main theme and [theme hook name] with html.

function themename_preprocess_html(&$variables) {

  $front_page = drupal_is_front_page();

  if ($front_page) {

    $variables['head_title'] = 'Jesús Heredia | Web Developer';

  }

}

Now that the title tag is ready, it's time for the description meta tag to be added.

Read the article number 10 of this series to learn how to add meta tags programmatically in Drupal 7.