My first WordPress plugin: HeadMeta

Mark Pilgrim was in the #wordpress IRC channel earlier, asking if there was some way to add <link> and <meta> tags to the <head> section of a page for specific posts. It seems he wants to control how various search engines and other bots index individual posts. I replied that he could do this using the custom fields feature.

A short while later, I had written my first plugin for WordPress: HeadMeta: .ZIP, .tar.gz, .phps, .txt.

To use this plugin, you add key/value pairs in the Advanced Editing interface. In particular, you’ll want to add keys named “head_link” and “head_meta”. The value will be plugged into the generated <link> or <meta> tag. For example, if you added the following keys and values:

Key Value
head_link rel=”seealso” href=”http://example.com/movies/”
head_meta name=”keywords” content=”entertainment,movies”

This would generate two tags in the <head> of the page, when someone visited the permalink for the post:


<link rel="seealso" href="http://example.com/movies/" />
<meta name="keywords" content="entertainment,movies" />

This will be useful for any situation where you want special <link> or <meta> tags generated for specific posts.

UPDATE 2005/07/14: I’ve updated the plugin to version 1.1, which adds a new feature: if you have a key named “keyword” or “keywords”, it will automatically generate a standard “keywords” meta tag. For example, if you have the following custom field:

Key Value
keyword pets, dogs, canines, training

The result would be:


<meta name='keywords' content='pets, dogs, canines, training' />

UPDATE 2005/10/15: I updated with a bugfix. The new version is 1.2.

UPDATE 2007/02/05: Version 1.3 is the newest version, which is now WordPress 2.1 compatible (it should still work with older versions, as well). Thanks to Gerald for pointing out the problem!

Stumble It!
My first WordPress plugin: HeadMeta

Related posts:

  1. HeadMeta Update
    " Prompted by some recent comments, I have made a minor update to my HeadMeta Plugin. There are now links to the color-coded PHP source,..."
  2. HeadMeta 1.4
    "Due to changes in the postmeta API, my HeadMeta plugin had a compatibility problem with some recent versions of WordPress. The problem appeared when there..."
  3. XCache Object Cache Plugin for WordPress 2.5+
    " Download: xcache-plugin.zip Version: 0.7d Updated: August 29, 2008 Size: 1.94 KB Powered by Drain Hole This is another one of those articles that will..."
  4. Text Filter Suite Plugin for WordPress
    "Since Talk Like a Pirate Day is only three weeks away, I spent some time this weekend revamping my old Fun Filters hack. The result..."
  5. Plugin: SpamForceField
    " SpamForceField is my newest anti-spam plugin for WordPress. This one does a couple of interesting things: All connections via pinappleproxy are denied. Every connection’s..."
This entry was posted in Plugins, WordPress and tagged , , , . Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

77 Comments

  1. Dan danielmoravec.com
    Posted November 28, 2006 at 11:34 pm | Permalink

    I updated your code to have the ability to use the key “description” and the value to be whatever you want the description to be. Contact me if you want it.

  2. Little Money littlemoney.net
    Posted January 12, 2007 at 3:03 am | Permalink

    Cheers for making this plugin! I find it very useful! :)

    -John

  3. Posted January 19, 2007 at 7:27 am | Permalink

    I have problems using it with wp 2.0.7 – it inserts the values correctly, but when generating the page / post for viewing it seems not to pull out the values, the generated page does just not contain any keywords meta tag..

    any ideas?

  4. Dougal dougal.gunters.org
    Posted January 19, 2007 at 12:50 pm | Permalink

    ovidiu: are you using a custom theme? If so, make sure that its header calls the wp_head action. See above for more details.

  5. Posted January 21, 2007 at 6:42 am | Permalink

    well, header.php does include a call to wp_head…. I guess another plugin is interfering. Your plugin was the first I used for this but as I had problems, I tested a couple of others and to be honest 3 other s didn’t work too, so I guess its another plugin I am using that interferes…

    I will just test your plugin again in a new blog, when needs arise, and test it first, before activating other plugins. fyi the theme I am using is the apart theme from gluedideas.

  6. Posted January 30, 2007 at 12:04 pm | Permalink

    Thanks, Dougal – I like that plug-in a lot!

    I noticed that it does NOT work any more in WP 2.1 – unless you modify lines 46 and 47 of headmeta.php. The modfied lines are:

    [46] $metavals = get_post_meta($id, ‘head_meta’, false); // $post_meta_cache[$id]['head_meta'];
    [47] $linkvals = get_post_meta($id, ‘head_link’, false); // $post_meta_cache[$id]['head_link'];

    I have kept the original code as a comment, just in case.

    This is due to the fact that “as of WordPress 2.1, $post_meta_cache isn’t populated anymore.”, see http://codex.wordpress.org/Using_Custom_Fields for detailled information.

  7. Dougal dougal.gunters.org
    Posted February 5, 2007 at 12:34 pm | Permalink

    Now WordPress 2.1 compatible.

  8. Matthew dangerouslyfun.com
    Posted February 17, 2007 at 6:18 pm | Permalink

    I noticed that if you already have meta description in your header.php file and you use this then both descriptions show up. Does that cause any problems with search engines? Which description will they prioritize?

    Thanks – great plugin!

  9. Posted February 26, 2007 at 3:29 pm | Permalink

    i downloaded the vital plugin + installed it. But I get always the error : “parse error, unexpected T_RETURN in…”

  10. Joe partner-source.de
    Posted April 26, 2007 at 4:33 am | Permalink

    It doesn work!!! :-( ((

  11. Dougal dougal.gunters.org
    Posted April 26, 2007 at 12:31 pm | Permalink

    It doesn work!!! :-( ((

    Can you be a little more vague, please? :)

  12. manele manele.fm
    Posted August 31, 2007 at 10:56 am | Permalink

    thx for this very very useful plugin!

  13. Posted November 11, 2007 at 10:24 am | Permalink

    Your Plugin Headmeta throws a fatal error with my WP 2.3.1 installation. Is there any way to track the error, since you say that it is compatible with that version?

    Matthias

  14. dieta bogutynmlyn.pl
    Posted December 18, 2007 at 8:18 am | Permalink

    Does anyone know of any issues with this plugin and WP version 2.0.7?

  15. Firepow hubpages.com
    Posted May 5, 2008 at 1:57 pm | Permalink

    I have found that this plugin still works after I updated my blog to 2.51 which is great news. I have found that this plugin does clash with some themes I use so it’s always worth checking this out. Apart from that this plugin totally rocks great job.

  16. malcolm coles malcolmcoles.co.uk
    Posted May 30, 2008 at 2:19 pm | Permalink

    You can now add meta data like keywords and description using Custom Fields in the latest version of wordpress, and the get_post_meta function. Still, you’d have to be able to work out a bit of PHP to do it, so perhaps a plugin is better!

  17. malcolm coles malcolmcoles.co.uk
    Posted May 30, 2008 at 2:23 pm | Permalink

    There was some PHP code at the link on my previous post, but the link has been truncated for some reason… Anyway, if anyone wants to see the PHP that I hadn’t even mentioned was there, click my name and look for it in the wordpress category …

  18. Richard philix.co.uk
    Posted July 23, 2008 at 6:10 am | Permalink

    Nice plugin and easy to use :)

  19. Posted August 29, 2008 at 10:02 pm | Permalink

    Check out Malcolm’s page in comment 66 and you are given the simple solution to use meta keywords and meta descriptions for your posts without using any plugins.

  20. Posted September 4, 2008 at 11:15 am | Permalink

    Awesome resource for tricks, glad I found this blog and there are hundreds of pages of info, awesome. The web is really a great place.

  21. Posted September 10, 2008 at 7:54 am | Permalink

    I am going to plug- in. I think this would help me in more ways than one. My brother has already used it and he says that it rocks! Kudos!

  22. Goran Web netage.co.za
    Posted September 23, 2008 at 10:55 am | Permalink

    Heads up, tried it out and it works like a charm, thanks again.

  23. Posted August 25, 2009 at 9:54 am | Permalink

    Hi! This plugin doesn’t seem to work in wordpress 2.8.3. I’ve installed and activated it, but anything is shown in the panel editor.

    • Dougal dougal.gunters.org
      Posted August 25, 2009 at 10:46 am | Permalink

      There’s not a settings page for the plugin. You just set Custom Fields on your post, and it will automatically add the appropriate headers when you publish it.

      • Posted August 31, 2009 at 1:51 pm | Permalink

        Oh! I understand now. I use the “Custom Fields” bought by default in the post editor panel of WP, setting datas for “head_link” and “head_meta”. Right?. Thanks you, it’s so useful for me. Montxo.

  24. Nick Aviles nickaviles.com
    Posted October 18, 2009 at 3:20 am | Permalink

    excellent post. but i dont think search engines, and google in particular, view keywords as much at all.

  25. internet reklamlar? netreklam.org
    Posted February 18, 2010 at 8:49 am | Permalink

    This script not work with WordPress 2.0

27 Trackbacks

Post a Comment

Your email is never published nor shared.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Subscribe without commenting

  • Subscribe

  • Follow Me

    Twitter  Facebook  Flickr  Last.fm  LinkedIn  StumbleUpon  Technorati  Delicious  
    • icon
  • Referrals

    Sign up for Text Link Ads and earn money from your blog.
  • Lifestream