WordPress: Dynamic Meta Description and Keywords howto
This is a quick guide to add dynamic meta description and keyword to each different url of our Wordpress blog, very useful to SEO users.
Open your header.php file from your theme directory and put this code into:
Meta Content Description
<meta name="description" content="
<?php if (have_posts() && is_single() OR is_page()):while(have_posts()):the_post();
$out_excerpt = str_replace(array("\r\n", "\r", "\n"), "", get_the_excerpt());
echo apply_filters('the_excerpt_rss', $out_excerpt);
endwhile;
elseif(is_category() OR is_tag()):
if(is_category()):
echo "Posts related to Category:
".ucfirst(single_cat_title("", FALSE));
elseif(is_tag()):
echo "Posts related to Tag:
".ucfirst(single_tag_title("", FALSE));
endif;
else: ?>
ADD HERE YOUR DEFAULT DESCRIPTION
<?php endif; ?>" />
Meta Content Keywords
<meta name="keywords" content="
<?php
$tags = get_tags(array('orderby' => 'count', 'order' => 'DESC'));
$xt = 1;
foreach ($tags as $tag) {
if ($xt <= 20) {
echo $tag->name.", ";
}
$xt++;
}
?>ADD, HERE, YOUR, STATIC, KEYWORDS" />
If you enjoyed this post, please consider to leave a comment or subscribe to the feed and get future articles delivered to your feed reader.

14:02 on December 8th, 2009
It worked great for one of my blogs (WordPress 2.7.1), but not for the other one, using Wordpress 2.0.5 (I know it’s old, but I am frightened of upgrading and crashing my whole site).
Do you have a similar script for these “old” WP versions ?
Thanks !
Sylvie
13:53 on December 8th, 2009
Génial ! Un grand merci ! Sylvie (Belgium)
06:16 on November 17th, 2009
very useful, thanks kindly
12:40 on October 1st, 2009
Very useful thank you!
19:34 on May 31st, 2009
С удовольÑтвием читаю вÑе ваши Ñтатьи. Продолжайте пиÑать обÑзательно. Ðндреич, БашкортоÑтан.
07:15 on May 31st, 2009
Lovely! Now I don’t need the Add Meta Description plugin—which works great, but I like being able to minimize the number of plugins on my blog.
02:14 on April 5th, 2009
Does anyone know where I can find free online virus scanners?
07:56 on March 3rd, 2009
sorry, that didn’t come through right – I mean ‘ and ’ etc.
07:53 on March 3rd, 2009
Oh, Austin Web Design – just thinking, rather than removing them, might it not be better to convert single and double quotes to their character entities? (i.e. ‘ ’ etc.)?
07:43 on March 3rd, 2009
Thanks very much for this Michele. I realised this morning that all my pages had the same meta description and hence my site looked pretty stupid in Google – with your code snippet above I have now easily fixed this. Very much appreciated.
One change I have made: your code above removes all newlines, but I have changed this to replace them with a space.
get_the_excerpt replaces any s in your content with newlines (via a call to strip_tags). If you simply remove the newlines, then you end up with sentences that run straight into one another without a space.Like this.Which is ugly.
(Most of my pages start with a one-line paragraph, so this was particularly relevant to me).
For anyone else interested, I changed:
str_replace(array(“\r\n”, “\r”, “\n”), “”, get_the_excerpt());
to:
str_replace(array(“\r\n”, “\r”, “\n”), ” “, get_the_excerpt());
thanks again – very much appreciated
02:10 on January 30th, 2009
Michele, this is a great resource. I added single and double quotes to the list of replaced characters in the excerpt. I also took out the “[...]” at the end of the excerpt.
Thanks so much.
06:33 on December 8th, 2008
wonderful code. thanks for the share.
15:50 on November 23rd, 2008
thanks man for this , really need it . check my website http://www.axxomovies.org
03:31 on November 6th, 2008
mdyayayaya ….. * Many people think *…. the author thanks for the post!
18:31 on October 30th, 2008
Thanks boy! now i can try implement code…! ;)
19:29 on October 14th, 2008
Thank you for this tips.
It’s good for google :)
21:11 on August 29th, 2008
Hey =) Just check this out: Tila Tequila is naked uhhhh…. hot bitch: http://tilatequilanaked.0catch.com/
12:20 on August 28th, 2008
Thank you, thank you, thank you.
07:11 on August 12th, 2008
Thank you, thank you, thank you! This was code was exactly what I was looking forward to! Thank you for sharing!
14:38 on August 6th, 2008
Keywords aren’t useful for SEO any more. That’s what Google Guys say.