The Ultimate Meta Description Function for WordPress

Just like with title tags, I like better control over my meta descriptions than WordPress provides right out of the box (which is actually none whatsoever with the default theme).

If you want custom meta descriptions on your site, give your posts a custom field of meta_description and whatever value you want the description to have (ie, the copy you want for the description).

Then add the following to your theme’s functions.php:

function click_the_meta_description() {
if (is_singular()) {
if ($meta_description != '') {
echo $meta_description;
}
else {
global $post; setup_postdata($post);
$meta_description = get_the_excerpt();
echo $meta_description;
}
}
elseif (is_search()) {
echo 'This page shows search results for the query: '.get_search_query();
}
elseif (is_category()) {
$category_description = category_description();
$category_description = strip_tags($category_description);
$category_title = single_cat_title('',false);
if ($category_description != '') {
echo $category_description;
}
else {
echo 'This page shows all posts filed under the category '.$category_title;
}
}
elseif (is_archive()) {
$archive_period = 'Archive for '.get_the_date( $d ).' | '.get_bloginfo('name');
echo 'This page shows content created during the time period of '.$archive_period;
}
else {
bloginfo('description');
}
}

Next, open up your header.php file and insert the following (or overwrite any other code generating a meta description):

<meta name="description" content="<?php click_the_meta_description(); ?>">

Up Next: The Ultimate Title Tag Generator for WordPress