Custom Meta Descriptions with WordPress
By: Matthew Blevins, August 7th, 2007
There are now a variety of plugins that offer variations on the concept of customized post descriptions, keywords, etc., etc., but I’ve found that there’s always at least one thing about the plugin that I didn’t like. Before any of those plugins existed (or at least before I knew they existed), I had our PHP coder create some customized code for our blogs that would allow me to enter descriptions for each individual post, as well as to write a custom description for the blog home page and custom descriptions for tag pages and category pages. It’s a quite useful tool to be able to do this, especially from an SEO perspective, and the code that is required is surprisingly simple. Now that I know how to hack up PHP code a bit, I feel perfectly comfortable adding that original coding to new blogs, and I’ll show you how to do it here as well.
Here’s the code itself:
<meta name=”description” content=”<?php
$META_DESCRIPTION = “YOUR HOME PAGE DESCRIPTION HERE”;
if (is_tag())
{
print “Posts tagged “;
print ucwords(str_replace(array(”-”,”_”,”+”),” “,get_query_var(”tag”))).” on “;
bloginfo(’name’);
}
else if (is_single())
{
$meta_desc = get_post_meta($wp_query->post->ID, “page_description”);
print htmlspecialchars($meta_desc[0]);
}else if (is_category() and category_description())
print strip_tags(category_description());
else if (is_home() and $META_DESCRIPTION)
print $META_DESCRIPTION;
else
bloginfo(’description’);?>” />
Now, this code is to be inserted in the “header.php” file in your WordPress theme and should be within the <head> </head> tags. I usually place this right after the <title></title>. Also, let me remind you that if you’re not using tags, or if for some reason you don’t use categories, there are aspects of this code that won’t work for you. If you DO use categories to organize posts, and you are using the UltimateTagWarrior plugin, you can insert the code above into the header of your blog, edit the home page description field, and you’re set!
However, you will need to be aware of how to actually add the descriptions for categories and posts (it’s quite simple, I assure you). We’ll start with Categories:
Click on “Manage > Categories” from the dashboard. You should then see a list of categories. Click the “Edit” link next to any category that doesn’t already have a description, enter a meta description in the “Description (optional):” field and hit the “Edit Category >>” button. That’s it! If you’ve done that and placed the above code in yoru header, that description will now be the meta description for that category page.
Adding post Descriptions:
Adding descriptions to posts takes a little more getting used to, but it’s still quite simple. In the “write post” page for WordPress, click on the “+” sign to the right of the “Custom Fields” area to open that dialogue box. In the “Key” field, place “page_description” (EXACTLY like that, and without the quotes) and then enter a meta description for the post. Thereafter, hit the “Add Custom Field” button. You can then save/publish your post and the custom description you entered will be associated with that post (again, assuming you entered the code from above in your header).
In the future, you will not have to enter “page_description” in the “Key” field, but instead can select it from the dropdown box to the far left of the “Custom Fields” area.
That’s it! Hope you’ll all enjoy the tip and use it to your advantage in your blogging pursuits.
Tags: blog seo, php code, seo, wordpress, wordpress hacks
