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.

17:35 on August 23rd, 2012
I never comment on these things but this was so helpful that I just have to say “thank you” and great post.
16:06 on February 9th, 2012
thanks for such a nice tip..
15:59 on February 6th, 2012
thanks bro.. ur sharing is useful for me..
16:28 on January 20th, 2012
nice share…thanks you
02:57 on September 18th, 2011
It’s appropriate time to make a few plans for the long run and it’s time to be happy. I have read this put up and if I could I want to recommend you few interesting issues or suggestions. Maybe you can write next articles referring to this article. I desire to learn more issues about it!
00:39 on July 29th, 2011
[...] to a handy Google search, I came across this site, which has the code snippets nessesary to auto-generate META information. I did change one thing in [...]
13:19 on April 5th, 2011
[...] ein bisschen Suche habe ich zwei Beispiele gefunden, die auch wirklich funktionieren. Das erste Beispiel ist in meinen Augen etwas zu viel Code. Das zweite Beispiel kommt mit deutlich weniger Code zu [...]
20:51 on March 5th, 2011
Old post, but great solution, Thanks a ton. It works perfect. solved my SEO problem
————————————————————————————
Technology Enlightened
09:04 on January 7th, 2011
hi thanks for great post if you don’t mind please can you tell me which plug-in you use in your blog for similar post.
19:01 on June 30th, 2010
Hi, good post, thanks.
If I visit a website I get attracted by a soothing design, this actually instills the confidence in me to turn more pages from the site.
22:22 on June 21st, 2010
Thanks so much! That works great.
17:01 on June 3rd, 2010
thx 4 the post ;)
19:17 on May 4th, 2010
I’m trying to use Facebook’s new Like Button feature on my site and it’s working pretty good but when adding a few more pieces of Facebook Like meta data to the post pages I found I would do well with adding the description as well in the following format:
When I add your code into single.php with conditional statements (post page?) the following shows up in the HTML Source:
Help????
15:49 on April 16th, 2010
[...] Add dynamic description and keywords meta tags in WordPress April 16, 2010 | Posted by admin (online) http://www.michelem.org/2008/07/11/wordpress-dynamic-meta-description-and-keywords-howto/ [...]
00:04 on March 20th, 2010
oops the code didn’t paste, lets see if this works:
content=”
asdf”
00:03 on March 20th, 2010
Great stuff, thanks for the tips, just one question, in the meta description tag there is a newline after the content parameter like this:
Is it possible to remove that newline? Just curious, I’m guessing it’s got something to do with the str_replace but I’m not sure
Thanks!
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!