WordPress Description META Tag

Published 23rd August 2010

According to this post WordPress does not include description and keyword meta tags by default. Their reasoning for this is simple: the information is unique to your site and should therefore be included by yourself.

Now this is fine except when you forget to add your own meta tags or you just can’t be bothered adding them yourself (which is bad for your SEO). So below is a simple bit of code to put in your header.php file wherever you would normally put your meta tags.

<?php if( is_single() || is_page() ){ if( have_posts() ){ while( have_posts() ){ the_post(); $excerpt = htmlentities(strip_tags(get_the_excerpt())); if( strlen($excerpt) > 140 ) $excerpt = substr($excerpt, 0, 137) .'...'; echo '<meta name="description" content="'. $excerpt .'" />'; } } } elseif( is_home() || is_front_page() ) { echo '<meta name="description" content="'. get_bloginfo('description') .'" />'; } ?>

All this code does is add the meta “description” tag to your single posts/pages and to your homepage. The content gets filled in automagically. If it’s a post or a page the content will be the first 140 characters of the excerpt of the post. If it’s your homepage then the content will be the site description. Easy.

I shouldn’t need to mention that if your even the slightest bit interested in your sites SEO then including meta tags is a must, and this is an easy way to do it. Let me know what you think.

Update

I’ve written a beefed up version of this code at Ultimate WordPress Description META Tag Code.