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" />
Tags: , , , , , , ,

Post simili:

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.

29 Responses to “WordPress: Dynamic Meta Description and Keywords howto”
  1. Graphic Design Tutorials
    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.

  2. Nicolas Ritoux
    22:22 on June 21st, 2010

    Thanks so much! That works great.

  3. weidhas
    17:01 on June 3rd, 2010

    thx 4 the post ;)

  4. morestar
    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????

  5. Add dynamic description and keywords meta tags in Wordpress | WithExample
    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/ [...]

  6. david
    00:04 on March 20th, 2010

    oops the code didn’t paste, lets see if this works:

    content=”
    asdf”

  7. david
    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!

  8. Capucine
    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

  9. Capucine
    13:53 on December 8th, 2009

    Génial ! Un grand merci ! Sylvie (Belgium)

  10. wordpresswell.com
    06:16 on November 17th, 2009

    very useful, thanks kindly

  11. Simone
    12:40 on October 1st, 2009

    Very useful thank you!

  12. myukaht
    19:34 on May 31st, 2009

    С удовольствием читаю все ваши статьи. Продолжайте писать обязательно. Андреич, Башкортостан.

  13. Jessi Hance
    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.

  14. C Jones
    02:14 on April 5th, 2009

    Does anyone know where I can find free online virus scanners?

  15. Ben PC
    07:56 on March 3rd, 2009

    sorry, that didn’t come through right – I mean ‘ and &#8217 etc.

  16. Ben PC
    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.)?

  17. Ben PC
    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

  18. Austin Web Design
    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.

  19. Webmaster Forum
    06:33 on December 8th, 2008

    wonderful code. thanks for the share.

  20. axxo
    15:50 on November 23rd, 2008

    thanks man for this , really need it . check my website http://www.axxomovies.org

Leave a comment

(required)

(required)


Comments links could be nofollow free.

Categories