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.

4 Responses to “Do it Yourself: Optimize your Site Titles”

  1. Gravatar

    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!

  2. Gravatar

    I use kind of the same thing in my blog.

  3. Gravatar

    I did better, I am writing a book :)

  4. Gravatar

    You should write an ebook about creating wordpress plugins.

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="">