Enhancing the WordPress Title Element

Posted February 5th, 2007 by Mike Cherim

I have written about page titles in WordPress before, but when I made my new theme I found another alternative. I feel this method provides slightly better results across a wider range of installations.

In the header.php file you need to look for the title element. It looks like this: <title></title>, but this is the element only, shown without its content. What you’ll probably see is this code (on one line):

 <title>
  <?php bloginfo('name'); ?>
  <?php if ( is_single() ) { ?>
   » Blog Archive <?php } ?>
  <?php wp_title(); ?>
 </title>
 <!--This would normally be shown on one line but doesn’t have to be-->

With my improvements more information will be shown, including search terms on the search results page and a title error for the error page (404.php).

 <title>
   <?php bloginfo('name'); ?> &raquo; <?php bloginfo('description'); ?>
   <?php if(is_home()) { ?>
     &raquo; Home Page
   <?php } ?>
   <?php if(is_single()) { ?>
   <?php } ?> <?php wp_title(); ?>
   <?php if(is_404()) { ?>
     &raquo; 404 Error! Page Not Found
   <?php } ?>
   <?php if(is_search()) { ?>
     &raquo; Search Results for: <?php echo wp_specialchars($s, 1); ?>
   <?php } ?>
 </title>
 <!--This would normally be shown on one line but doesn’t have to be-->

To use this, copy the code shown and paste it into the header.php file where your title element currently lives. That’s it.

Update:

Due to the great feedback I got on this post I updated the title script and recommend this one. In mine, unless you edit a core function file, replace instances of “-” with “&raquo;” in the scripting shown.


7 Responses to: “Enhancing the WordPress Title Element”

  1. Adam H responds:
    Posted: February 5th, 2007 at 6:58 pm

    I think the SEO guys won’t like this one too much. The dangers of a really long page title is search engines only show a set amount of characters and cut the rest with an ellipsis. For example your search results for this blog don’t have relevant titles that would draw the user to click when compared to other results for a given search string.

    A good example of a well branded title tag would be that of Wikipedia. Check out the search results for SEO. It would be easy to rearrange your example to the form: H1 title - Site Brand, Site tag-line. That way the unessential tag-line is the first to go.

  2. David Zemens responds:
    Posted: February 5th, 2007 at 7:36 pm

    Another slick idea, Mike. Thanks much.

  3. Rich Pedley responds:
    Posted: February 7th, 2007 at 6:43 am

    Ignoring the fact that I hate long title’s, is this good for accessibility? I’m not so sure it is. Isn’t a page title read out when a user visits a page using JAWS (to pick on screen reader at random).

    Short and concise title’s may be better and more friendly.

    In case you ask why I don’t like them - just add the page to your bookmarks, most of mine are short and concise, I have very very few that are that long. Editing everyone becomes a pain!

  4. Optimizing WordPress for Search Engines » Beast-Blog.com responds:
    Posted: April 18th, 2007 at 9:57 am

    […] What I suggest in most instances is to use this element to house the site name and page title. With WordPress I suggest the title element be used to house the blog/site name and blog/site description on the main page — and that the description be carefully thought out (more so than what I use it for on my blog). I also suggest that the page title on pages other than the main page come first as this produces a better result of a Google search page since only so much information is shown (as suggested by Adam Hewgill in this post comment in response to a prior post on this subject). Here’s what I am offering: […]

Sorry. Comments are closed.




Note: This is the end of the usable page. The image(s) below are preloaded for performance only.