WordPress SEO Tip – Custom Page Descriptions, Without Plugins!

Google and the other search engines are getting more intelligent & more useful all the time, but it’s no secret that they could use a little help every now and again.

Take this as an example:

What you’re looking at is an image taken from the first page of the search results for site:rickbeckman.org.

Your results will vary based on the layout and semantics of whatever theme you’re using; I’m currently using the incomparable Thesis, and Google is using as each post’s description the same thing on every page: “Post Title. by Author on Date. First line of the post.”

That isn’t exactly brilliant marketing. Just what exactly does this tell you about the article itself: “Socializing Public Schools. by Rick on August 2, 2008. The other day at work, I was looking through the various school supply lists that our stationary …”?

Thankfully, WordPress provides a very easy way to provide Google & other search engines with a more intelligent, more appropriate description statement, without the use of a plugin!

Step 1: The Function

Rather than installing a plugin, I will be giving you a short snippet of code which you will in turn need to add to your theme’s functions.php file (in Thesis, this will be placed in /custom/custom_functions.php instead). If this file does not exist, go ahead and create it in your theme’s directory; make sure that it is saved as a plaintext file (use Notepad as opposed to Word or Works, for example).

This is the code you’ll want to add to the file:

// Meta descriptions for posts w/ the Description custom field
function descriptification() {
	// This is only relevant on pages or posts
	if (is_single() || is_page()) {
		// We'll need the post ID, so make $post available
		global $post;
		// Check to see if a post has a custom field with key of "description"
		if (get_post_meta($post->ID, 'description', true)) {
			// We have a description, so output in the form of an HTML META tag
			echo '<meta name="description" content="' . attribute_escape(get_post_meta($post->ID, 'description', true)) . "\" />\n";
		}
	}
}
// Set descriptification() to run when wp_head() is called, generally in a theme's header.php file
add_action('wp_head', 'descriptification');

Ensure that code is placed in between the opening & closing PHP tags ([code]<?php[/code] & [code]?>[/code]). The code above is commented throughout so you can see just what it’s doing.

Step 2: The Describing

Setting up your blog to output a description isn’t going to do you any good unless you start adding descriptions to your posts.

The above code outputs custom field values which are keyed as descriptions, so that’s what we’ll want to create.

When editing a post or page, scroll down a way until you get to the Custom Fields section. Add a new one with key “description” and value as whatever best describes this particular page or post. Keep your description concise — remember, it’ll show up in search results, and you don’t want it getting trimmed because it’s too long — but be sure you’re emphasizing the subject matter.

Press the “Add Custom Field” button, save your post, and visit it to make sure everything went smoothly. View the source of your post or page after you’ve added a description, and you should see the meta description tag being output somewhere in the header area.

WordPress 2.6.2 has a bug which affects custom field values which contain certain symbols: ', ", and \ are the ones I know to be affected. What happens is that when you initially press the “Add Custom Field” button, too many slashes are added to your custom field’s content, which you’ll see when the custom field shows up in the list of added fields. ', ", and \ become \', \", and \\, which is highly undesirable. Once you add the custom field, you can edit out the slashes and save it. A minor inconvenience, but a bug nonetheless — one which should be fixed in whatever the next version of WordPress turns out to be.

This is a rather minor modification, but it could result in noticeable improvement in your search engine results placement. You’ll need to give Google time to get a hold of your new descriptions, though, so don’t be surprised if you see the old, less-than-useful descriptions in results for your site for a while to come.

If you found this post useful, you may want to consider subscribing to post updates or following me on Twitter. Happy Talk Like a Pirate Day, ye scurvy bilge rats!

7 thoughts on “WordPress SEO Tip – Custom Page Descriptions, Without Plugins!”

  1. Great tip, Rick. Even better, you motivated me to finally make use of the Headspace plugin that I had already installed on my blogs. For me, the main advantage it has over your tip is the ability to edit metadata in bulk, which was handy since I was missing descriptions from all of my posts.

    But Headspace is also rather intimidating in the number of features it exposes, and its rather limited documentation. Whereas your tip is well documented and targeted at the only feature of Headspace that I’m actually using (adding descriptions to posts). Nicely done.

  2. christine: Well, it looks like your site’s biggest problem is that Google is giving a higher priority to your tag archives than to your actual posts. That’s generally a bad thing — you want people to land on your individual posts when coming from search engines, not a tag archive loaded with content for which they weren’t actually searching.

    I’m by no means an expert on search engine optimization — thankfully, the theme I’m using (Thesis by DIYthemes) is doing most of the hard work — but I’m learning. :)

    HeadSpace2 looks like quite the behemoth. I’m sure one of it’s 8,004 features, it’ll take care of site descriptions. :) However, depending on the method HeadSpace2 uses to store descriptions, it may not be compatible with the code I posted above. In other words, if you use both methods, you could wind up with a description output via my method and another via HeadSpace2’s. There isn’t a real advantage of one or the other — mine has less overhead; HeadSpace2’s may be slightly easier to implement.

    But yeah, certainly, we can talk more about it next time we’re both online.

  3. Rick,

    We need to have a serious talk. I googled my site name. Once I got over the embarrassment at the mixed up jumble that appeared, I re-read this article about 8 times. I don’t even know what my site is outputting. It looks like a mess of random tags and category names. So, to begin with, what did your site have before you put in the new code to produce your articles in such a nice ordered way on a Google search?
    And then, what am I going to do to improve ? I was using the description custom field for awhile but didn’t know how to verify what was happening. I’ll put that piece of code in and do the descriptions again. Feel free to come visit and critique my efforts – you’re always welcome and appreciated : )
    The Headspace plugin that Patrick mentions sounds rather good since I also need to go back and do a few posts. Do you recommend it?
    Seriously useful article. Thanks.

Leave a Comment

Your email address will not be published. Required fields are marked *

Use your Gravatar-enabled email address while commenting to automatically enhance your comment with some of Gravatar's open profile data.

Comments must be made in accordance with the comment policy. This site uses Akismet to reduce spam; learn how your comment data is processed.

You may use Markdown to format your comments; additionally, these HTML tags and attributes may be used: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

This site uses Akismet to reduce spam. Learn how your comment data is processed.

the Rick Beckman archive
Scroll to Top