WordPress Titles & Descriptions for SEO

Posted November 5th, 2006 by Mike Cherim

I recently made some modifications to this blog to help get its articles indexed more quickly (for SEO reasons), to help users identify one article or page from the next with greater ease, and to make it easier for people to Digg articles, mark them in Ma.gnolia, etc. If you see your browser’s title bar you’ll now notice that the individual article pages show the name of the article more clearly, and on the “blog pages” the position of the page name is better. You’ll also see I modified the title separators. These modification also apply to the META “description” as I tried to make them more distinctive as well (good article titles help). It was pretty simple. Here’s what I did and how can do it, too.

The Header Elements

First thing to do is, via the Theme Editor (Presentation page), access the header.php file. Once there you’ll see the title and META description in the head. Here’s what you’ll be editing in the head of your blog.


 <title>Enter code here</title>

 <meta name="description" content="Enter code here" />

Quick Configuration Block

But before you do that you’ll want to add this configuration block at the top of the header.php file — somewhere above the title element:


 <?php
  // CONFIGURE
     $blog_auth  = 'Author\'s Name';
     $blog_longd = 'An informative long main description for your blog\'s main page';
 ?>

 Note: Do not enter quotes and be sure to escape any word\'s apostrophe, as demonstrated above.
 Note: *This hack is designed for a single author blog. (Info)

Taking Care of the Title

Now, place this code block between the title tags:


 <?php
 if ( is_single() ) {
      bloginfo('name');
      wp_title();
      echo (' by '.$blog_auth.'');
 } else if ( is_page() || is_paged()  || is_author() ) {
      bloginfo('name');
      wp_title();
 } else if ( is_category() ) {
      bloginfo('name');
      wp_title();
      echo (' Category');
 } else if ( is_archive() ) {
      bloginfo('name');
      echo (' - Archives');
      wp_title();
 } else if ( is_search() ) {
      bloginfo('name');
      echo (' - Search Results');
 } else if ( is_404() ) {
      bloginfo('name');
      echo (' - 404 Error');
 } else if ( is_home() ) {
      bloginfo('name');
      echo (' - ');
      bloginfo('description');
      echo (' - Blog Home');
 } else {
      bloginfo('name');
      echo (' - ');
      bloginfo('description');
 }
 ?>

Taking Care of the Description

Then you need to add this code block between the META description content quotes:


<?php if ( is_single() ) {
      bloginfo('name');
      wp_title();
      echo (' by '.$blog_auth.'');
 } else if ( is_page() || is_paged()  || is_author() ) {
      bloginfo('name');
      wp_title();
 } else if ( is_category() ) {
      bloginfo('name');
      wp_title();
      echo (' Category Archive Page: '.$blog_longd.'');
 } else if ( is_archive() ) {
      bloginfo('name');
      echo (' Dated Archives Page: '.$blog_longd.'');
      wp_title();
 } else if ( is_search() ) {
      bloginfo('name');
      echo (' Search Results Page: '.$blog_longd.'');
 } else if ( is_404() ) {
      bloginfo('name');
      echo (' 404 Error (Page Not Found): '.$blog_longd.'');
 } else if ( is_home() ) {
      bloginfo('name');
      echo (': ');
      echo (''.$blog_longd.'');
 } else {
      bloginfo('name');
      echo (': ');
      echo (''.$blog_longd.'');
 }
 ?>

Good Riddance &raquo;

Okay, one last thing if you want it looking just like mine. In other words you don’t want to have a “»” (&raquo;) in the title bar and want to get rid of it once and for all so as to provide something more semantic, here’s what you need to do.

  1. Via FTP or your server control panel, find the file template-functions-general.php. This file is found in the /wp-includes/ folder/directory.
  2. Open that file and scroll down to Line 139 (this assumes you have WordPress version 2.0.5 as it may be on a different line in other versions, but it’ll be in the general area).
  3. On that line you’ll find this code string:
       function wp_title($sep = '&raquo;', $display = true) {
  4. Change it to this:
       function wp_title($sep = '-', $display = true) {
  5. Save the change. You’re done. Test it.

Tah-Dah!

There’s probably already a plugin that does this for you, but this is how I did it. This was a pretty easy fix. *I tried picking up the post author, but for some reason I couldn’t figure out how (I’m still learning the ins and outs of WordPress). If someone knows how to do that please post the solution, then this will be suitable for multi-author blogs. If you don’t want to show an author, use that spot for something else… a company name perhaps.

Please note that this could be done with leaner scripting as the data is closely redundant and could be made to match exactly so one code block could be used for both. I didn’t bother trying to optimize this and decided to make the description a little fuller on most page templates. There are other template tags that could also be used here conditions used to be even more specific.

Update: Want more control over output, add what was suggested by Joe Dolson. Check out this revised version. Thanks Joe!


10 Responses to: “WordPress Titles & Descriptions for SEO”

  1. Joe Dolson responds:
    Posted: November 5th, 2006 at 12:30 pm

    I did meta description by using the Custom fields feature - just added a custom field in the document called “description”, then used this code:


    post->ID,'description',true); ?>" />

    That way I can fully customize the description for each document. I’ve also done title tags the same way, for sites where I needed the title tag to be something other than the full post title.

  2. Joe Dolson responds:
    Posted: November 5th, 2006 at 12:33 pm

    Yeah, that’s not actually the code I used…first half of it got stripped out.


    <meta name="description" content="<?php echo get_post_meta($wp_query->

    Then the rest of it above…

  3. Dennis Lembree responds:
    Posted: November 6th, 2006 at 12:00 am

    Excellent article, Mike, great fix. I implemented your code/suggestions in a matter of minutes! You’d think such a great blogging package like WordPress would have better SEO techniques build into it, at least for the meta data portion…

  4. Mike Jolley responds:
    Posted: November 6th, 2006 at 5:14 pm

    Nice technique. Personally, I cheated and used the plugin lol

  5. JDM responds:
    Posted: November 6th, 2006 at 8:02 pm

    Nice post. I’m experimenting with PHP and your examples helped.

  6. Anthony Brewitt responds:
    Posted: November 27th, 2006 at 5:57 pm

    I admit I cheated too. But you are right my WP plugin doesnt do descriptions!

  7. guerr responds:
    Posted: January 6th, 2007 at 10:36 am

    if your blog is popular (many visits) this means too many additional db queries for an “insignificant” purpose, i think :(

Sorry. Comments are closed.




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