Who doesn’t want a meaningful title for his/her Web site? Those of you who are aware of the importance of the <title> tag will take a good care of their code to properly render content inside this element.

When it comes to WordPress, working inside <title> is a bit of art. It is highly dynamic, customized and there are different ways of writing it. The WordPress team has recently made a recommendation with regards to properly using the wp_title() function. I quote the codex:

The wp_title() function should not be used by a theme in conjunction with other strings or functions (like concocting with bloginfo(‘name’) ) to set the title because it will render plugins unable to rewrite page titles correctly. The best practice is to use the wp_title filter with a callback function. This method is now a requirement for themes.

Something like <title><?php bloginfo(‘name’); ?> | <?php wp_title(); ?></title> inside header.php is a bad practice, because it prevents a plugin to alter the whole title. So we must use the wp_filter with a custom callback function.

Wiki Duh has written a how to, which I enhanced in order to properly deal with a situation where the user (theme developer) decides to position the separator to the right of the title. In my child theme’s functions.php file, I added the following code:

/* a custom callback function that displays a meaningful title
 * depending on the page being rendered
function elevenchild_filter_wp_title($title, $sep, $sep_location) {
  // add white space around $sep
  $sep = ' ' . $sep . ' ';
  $site_description = get_bloginfo('description');
  if ($site_description && (is_home() || is_front_page()))
      $custom = $sep . $site_description;
      $custom = $sep . __('Category', 'twentyeleven');
      $custom = $sep . __('Tag', 'twentyeleven');
      $custom = $sep . __('Author', 'twentyeleven');
  elseif(is_year() || is_month() || is_day())
      $custom = $sep . __('Archives', 'twentyeleven');
      $custom = '';
  // get the page number (main page or an archive)
    $page_number = $sep . __('Page ', 'twentyeleven') . get_query_var('paged');
  // get the page number (post with multipages)
    $page_number = $sep . __('Page ', 'twentyeleven') . get_query_var('page');
    $page_number = '';
  // Comment the 4 lines of code below and see how odd the title format becomes
  if($sep_location == 'right' && !(is_home() || is_front_page())) {
      $custom = $custom . $sep;
      $title = substr($title, 0, -2);
  // return full title
  return get_bloginfo('name') . $custom . $title . $page_number;
} // end of function elevenchild_filter_wp_title
/* add function 'elevenchild_filter_wp_title()' to the
 * wp_title filter, with priority 10 and 3 args
add_filter('wp_title', 'elevenchild_filter_wp_title', 10, 3);

Now in your theme or in your child theme’s header.php file, you can call wp_title() like this:

  <?php wp_title('|', true, 'left'); ?>
  // OR <?php wp_title('|', true, 'right'); ?>

Don’t hesitate to use the above code as is without any contribution to me. You are welcome to report bugs, modify the code and make corrections.