Do it Yourself: Optimize your WordPress Site Titles

The purpose of this article is to show technique and examples of optimizing your WordPress site titles by directly editing the theme header file, where the title tag is usually printed out.

By using this method you have greater control over the titles than any plugin can offer, so effectively you will be able to disable a plugin in the process, if you were using one (and one down is always good!).

Your title tag is usually found in header.php theme file. Here is the example code I use on my site.

<title>
<?php
global $post;
if ( is_single() || is_page() || is_archive() )
wp_title('');
else if(is_404())
echo '404 Error! Page Not Found';
else if(is_search())
echo 'Search for: '.wp_specialchars($s, 1);
else
bloginfo('name');
if (is_home())
{
echo ' | ';
bloginfo('description');
}
if ($post->post_parent)
echo ' - '.get_the_title($post->post_parent);
?>
</title>

Let's see what it does one by one.

Single pages and archives

if ( is_single() || is_page() || is_archive() )
wp_title('');

This code instructs that on singe post pages (is_single), pages (is_page) and archives (including categories, is_archive) default page title is written. This will be the post title or the category name. So my goal is to have only the specific phrase I have written, and nothing else.

404 Page

else if(is_404())
echo '404 Error! Page Not Found';

I have chosen simple Page not found title to let everyone know they are on a misplaced page.

Search results

If the user is on search results page, I want them to be able to quickly reference the search term by looking at the page title. This also helps search engines querying that page.

else if(is_search())
echo 'Search for: '.wp_specialchars($s, 1);

$s is the variable that holds the search term and wp_specialchars is a function for escaping printed text that you do not know in advance.

Other

In all other cases (such as home page) I simply print out my blog name.

else
bloginfo('name');

Home Page

if (is_home())
{
echo ' | ';
bloginfo('description');
}

In specific case of a home page, to the already printed blogname ('Vladimir Prelovac') I specifically add blog description to get 'Vladimir Prelovac | WordPress Expert and SEO Consultant' for my home page title.

Sub-pages

I have structured some of my pages like for example WordPress Plugins. This is the parent page for all my plugin pages.

When on a plugin page, the plugin name will be printed but I also want to print out the parent page name, in this case 'WordPress Plugins'. In this case I get for example 'Theme Test Drive - WordPress Plugins'.

if ($post->post_parent)
echo ' - '.get_the_title($post->post_parent);

To check if the page has a parent you can use check "$post->post_parent" which stores the ID of the parent page if it exists. To get the title of a post by ID use get_the_title(0 function.

Conclusion

By editing the theme file you have much better control of what the page title will be. This allows you to have much better search engine results with just a little effort, and also maybe turn off one plugin in the process. Note however that the change will remain strictly on the edited theme, and if you are the type who frequently changes themes this will not be a practical solution.

Continue reading:


Posted in: SEO, WordPress
TAGS:, , , , , , , , , , , , , , ,
Leave a comment

7 Comments

  1. dani
    Mar 25th, 2009

    Vladimir, it works on Home, Pages, Single pages, Archives, and Categories. But it won't work on frontpage paging. I need page title on title bar for the usability reason. Do you have any idea?

  2. ymm0t
    Feb 10th, 2009

    Nice article there
    helps so much!

  3. juliana
    Jan 21st, 2009

    Hi

    where do i find the code editing page in my dashboard?

    rds

  4. DemoGeek
    Nov 14th, 2008

    Can't wait for the book...your site seems to have a lot of easy to understand type of articles when it comes to WordPress. Well done!

  5. Banago
    Nov 12th, 2008

    I use kind of the same thing in my blog.

  6. Nov 10th, 2008

    I did better, I am writing a book :)

  7. Jim
    Nov 10th, 2008

    You should write an ebook about creating wordpress plugins.

Have your say

Your email is never published nor shared. Required fields are marked *

*
*

This site rewards regular commentators with do-follows links to their site.

Subscribe without commenting

About Vladimir

vladimir prelovac Hi! My name is Vladimir Prelovac. I am a computer engineer by profession and an adventurer by state of mind.

"I would love to change the world, I just don't have the source code yet."

Books by Vladimir

WordPress Plugin Devleopment Book WordPress Plugin Development: Beginner's Guide

Published by Packt Publishing, available online through Amazon. Click the image for more information.

Consulting Services

Professional WordPress solutions based on custom developed plugins and themes

Expert on-site WordPress SEO consulting and an 'out-of-the-box thinking' approach to problems