Inserting Meta Descriptions in WordPress
by Kyle Skrinak on Mar.28, 2009, under Technology
Another problem, another plugin? Managing CMS plugins can become its own cost center if you’re not prudent about selection and inclusion. Here is my most recent case in point. I need to add unique meta descriptions to WordPress. I’ve already added unique titles to this site via yet another plugin. So I found this page on adding one manually, which gets you 99% of the way there. He actually gets you all the way, but a tweak to his instructions brings it together. You can read why he established his conditionals like he did at that link — and you should read it. I would have commented the correction to his site but his comments are closed, so here goes.
This code works well for my purposes. This consolidates his two step-through examples, and I’ve brought the php start tag up to the html, otherwise, there’s a newline right after the opening double quote for the description text.
Setup:
- Establish a “description” custom field for your pages. (Perhaps your posts, too, I’m not too worried about those for now, since I’m using WP as a CMS, less so a blog.)
- Disable any server-side caching, if in use, which should be SOP, right?
- Modify your theme’s header.php file with the following in the meta section:
<meta name="description" content="<?php if (is_single() || is_page()) {
# Single post / page.
# Use the 'description' custom field.
echo get_post_meta($post->ID, 'description', true);
} elseif (is_category()) {
# Category page.
# Use category's description
echo trim(strip_tags(category_description()));
} else {
# Default meta description
# Blog's description
echo bloginfo('description');
} ?> " />

