Where to find Malcolm Coles, reviews, and tips on how to do things I couldn’t do.

malcolm coles


Unique meta description and meta keyword tags in your Wordpress themes 35

Posted on May 26, 2008 by Malcolm Coles

Meta description tag: update

Here is the meta description PHP with both category and tag pages included (and updated again to make them more than one word and to include monthly archives):

<meta name="description" content=

"<?php

if(is_home()) {echo ('Malcolm Coles: His blog for his reviews, so you can find him, and other stuff that takes his fancy.');}

else {if(is_category()) {echo category_description();}

else {if(is_tag()) {echo '-tag archive page for this blog' . single_tag_title();}

else {if(is_month()) {echo ' archive page for this blog' . the_time('F, Y');}

else {echo get_post_meta($post->ID, "Metadescription", true);}}}}?>">

I've given up adding keywords these days! Remember, as below, you need to add a custom field called Metadescription to get this to work.

Original post about meta descriptions and keyword tags

I know little about PHP. But after fiddling round with other people's example code, I've finally managed to use custom fields to get a unique meta description and unique meta keywords on each post, and on the home page.

To get this to work, you need to add new custom fields called Metadescription and Metakeywords to each post. The ones for the home page you hard code. And I'm still working on the category pages ...

Copy and paste the code below into the head section of the header bit of your wordpress template. It basically checks if it’s the home page. If it is, it uses the hard-coded meta description and meta keywords. If it’s not, it goes and looks up the custom fields and uses the values from there. So there we have it: a unique meta description and unique meta keywords in wordpress.

Unique meta descriptions in wordpress

<meta name="description" content="<?php if(is_home()) {echo ('PUT YOUR HOME PAGE DESCRIPTION HERE');} else {echo get_post_meta($post->ID, "Metadescription", true);}?>">

Unique meta keywords in wordpress

<meta name="keywords" content="<?php if(is_home()) {echo ('PUT YOUR HOME PAGE KEYWORDS HERE');} else {echo get_post_meta($post->ID, "Metakeywords", true);}?>">

You might also like
  1. Adding page numbers: How to avoid duplicate titles and meta descriptions in pages 2 and higher of Wordpress category and tag pages
  2. Avoid duplicate content with paged comments in wordpress
  3. Wordpress comment pagination & duplicate content
  4. Meta keywords tag: Bing says carry on but Google says it's a waste of time - but
  5. Wordpress and smart/curly quotes: turning them off

You should follow me on Twitter.

35 Responses to “Unique meta description and meta keyword tags in your Wordpress themes”

  1. Sumesh says:

    Some hacking there ;) but you don't have to jump through the hoops. Several plugins (eg: All In One SEO) offer this functionality and more without the hacking.

  2. Josh says:

    Thanks for posting mate and commenting on the headmeta plugin site. I was after something simple that didn't add "another plugin" to WP, something easily manageable, and this is it.

  3. Chris says:

    I've only recently started using WordPress but I was a little surprised to see that the meta description tag wasn't included as default. I suppose it could have taken the first paragraph or X characters as the text for the description but I can imagine this could create some pretty useless descriptions and you would prefer something more focused.

    I too like to dabble and have taken a similar route to yourself and added the tag in the code manually based on the page title functionality in themes/default/archive.php. I have nothing against plugins but this seemed too simple a request to require one.

    I suppose I'll have to be careful when it comes time to upgrade though but then again a plugin that works with the current version may not work after an upgrade either.

  4. A commenter says:

    Thanks, this is a great post... I improve lil bit to add meta description on tag archive, here the code:

    <?php if(is_tag()) {
    echo ''; } else {}
    ?>

    =)

  5. Samiha says:

    Great codes! Thank you very much Malcolm Coles

  6. erutrituts says:

    Отличный пост, прочитав несколько статей на эту тему понял, что всё таки не посмотрел с другой стороны, а пост как-то очень заинтересовал.

    • Courtesy of http://uk.babelfish.yahoo.com/translate_txt, this means "Outstanding post, after reading several articles on this theme he understood that all after all did not look from the other side, but post somehow greatly interested." Apparently.
      well, it did say that. When I switched host, all non-european characters got mangled. Something to do with unicode I imagine. So now it looks like gibberish

  7. Rick Upshaw says:

    I actually have the All-In-One SEO plugin installed. However, although I find it works incredibly well (and is available) for adding and editing POSTS, the plugin is nowhere to be found when I add or edit a PAGE. Does anyone know why this is, or what can be done about it? If the directions above are the only solution to this problem, then sobeit, I suppose, but if there's a way to make it work like it's supposed to, I'd like to try it.

    I am using version 1.4.6.11 of the SEO Pack, and version 2.7.1 of WordPress. The blog address is http://www.principia-college-football.org.

    Any ideas? Anyone?

  8. Jeremy says:

    Thanks for this post!

    I had figured I needed to use those custom meta fields to add the keywords and description to each page, but I just couldn't figure out the code to add to the templates.

  9. Jamal says:

    I have been searching for this code all day yesterday and finally figured out the phrase to put in the search engine and voila - up pops your site! Your instructions worked perfectly on all three of my sites! I wonder why all the theme developers don't include keywords anymore when some search engine tools still look for them.

    Thank you so much! Hopefully now people can find me on search engines.

  10. ann says:

    I thought it were a myth that some search engine tools are still looking for meta keywords. But I just learnt I were wrong about that. So I am really glad, I found this code. Just trying it. Works perfect. Thanks alot to Malcolm!
    BTW: It might be a bit of work to add new custom fields to each and every post. However, I prefer it that way. As the source code of the page looks much more tidy than it would be if I were using "all in SEO" (or similar plugins).

  11. Claire Walters says:

    Great little piece of code. works great! and I prefer not using plugins.

    It works on every site I have on my server using WP.
    However, I manage a WP site on a diff server & it will not work. this site has the same theme & plugins as another site that it is working on. odd. has to be a server issue?

    • Claire - I can't see what difference a server would make. Are you sure you've not missed out a quote mark or pointy bracket (or forgotten to set a custom field)?

      • A quick follow-up. knowing I use this code on many other WP sites - I dug deeper.

        Turns out a domain had not been redirected as I though it was by the client. once I got the domain issue figured out - no problems.

        this is by far the best little piece of code better than using a plugin anyday.

        thank you Malcom!

  12. Karen says:

    Thanks Malcom for this nifty piece of code. As a newbie to the world of html, php and css I am truly grateful to people like you who share their knowledge so freely and generously.

    Thank you!

  13. Scott says:

    A silly question...

    So you past the code into the header.php file in the field, but is it best to be at the beginning of the field, or just before the closing declaration?
    Not certain as to whether there is any significant difference or not, but simply wanted to clarify.

    • Claire Walters says:

      @scott I usually paste the code right under the title tag. not sure if where int he header makes any difference

  14. Scott says:

    @Claire

    Thanks :-)

  15. Karl Bedingfield says:

    Fantastic code!

    Just a quick question. I am trying to get this to work with specific categories but my code just creates a white page. What might I b doing wrong?

    Here's my code: http://paste-it.net/public/k1a1bcd/

    Any pointers greatly appreciated.

    Thanks,
    Karl

  16. Karl - I have to say I'm not a PHP expert - took me a lot of trial and effort to get this to work!
    But if you look at my code, the tag line is like this:
    else {if(is_tag()) {echo '-tag archive page for this blog' . single_tag_title();}
    whereas yours is
    elseif(is_tag()) { echo '-tag archive page for this Ely Herald' . single_tag_title();}

    The beginning is different - mine says:
    else {if(is_
    whereas yours is:
    elseif(is_
    So you're missing a space and also my { bracket has become a ( bracket in your code. At the very end I have a bunch of closing }}}}s - one for each if statement. You don't.

    Maybe try changing it so these things are different?

  17. Jonathan says:

    I am having great difficulty...not with your code, but difficulty in combining your code with existing code.

    THE ISSUE: I have a meta tag that defines specific post descriptions; however my homepage does not currentlty display the blog description because there isnt a home page per se, only an index holder for a custom home page display.

    So, the so called homepage doesnt display a description, it displays the description of the most recent post.

    I am hoping to use your code in combination to allow ONLY my home page to display the blog description, and then if its a post page to display the custom SEO description instead.

    So, how can we combine your code with the code I have to make this work?

    Here is my current code:

    ID, "_seo_description", true) ): ?>
    <meta name="description" content="" />

  18. Jonathan says:

    ** My last comment didnt display the whole code...and i removed all of the opening tags

    ?php if( get_post_meta($post->ID, "_seo_description", true) ): ?

    meta name="description" content="
    ?php echo get_custom_field('_seo_description'); ?"

    ?php else: ?
    ?php endif; ?

  19. Jonathan says:

    still not displaying... please see my source code at my website.




↑ Top