Skip to main content Accessibility Feedback

Better WordPress titles without a plugin

I hate using plugins for things that I can easily do myself with a few lines of PHP.

I’d been using the All-In-One SEO Pack plugin for years, but lately, I’ve only been using it to override the default page and post titles. This week, I dropped the plugin in favor of a few lines of code in my header.

First, a word about SEO

Many years ago, I was told it’s really important to include meta keywords in the header so that search engines know what’s on the page. After years of slimy SEO specialists gaming the system by loading sites up with irrelevant keywords in the header, sites like Google no longer pay attention to them. What about the description meta tag? That doesn’t really matter much either, although search engines often use that to display a few lines about the site in their results.

There is a new tag you can now include: link rel=“canonical”. This tells Google which URL for a site is the main one. This is particularly important on ecommerce sites that often have multiple URLs for one page, like and Setting the canonical will tell Google which of those two is the main URL. Not as important for blogs, but still worth including.

So the only two things that I really care about customizing are the title and the canonical. I’m ok with Google using the first few lines of a post as the description in their search results, except on the landing page, where I want something a bit more explanatory.

Changing the Title

In the header.php file, I replaced the default title content with this snippet of code courtesy of Todd Motto’s HTML5 Blank

<?php wp_title(''); ?><?php if(wp_title('', false)) { echo ' |'; } ?> <?php bloginfo('name'); ?>

Updated on March 5, 2013 with Todd’s snippet, which is way simpler than what I had been using before and achieves the same result.

Adding a Description

Underneath the title tag, I added this bit of code…

<?php if (is_home ()) : // if homepage ?>
    <meta name="description" content="<?php bloginfo( 'description' ); // Pulls site description ?>">
<?php endif; ?>

The if (is_home()) statement tells WordPress to only show this content on the landing page.

Adding a Canonical

This one’s crazy simple. Underneath the description if statement, I added…

<link rel="canonical" href="<?php the_permalink() ?>">

That simply uses the main permalink for the page as the canonical link.

This took less than five minutes and just a few lines of code, and allowed me to stop using a pretty big plugin.