Dougal Campbell's geek ramblings

WordPress, web development, and world domination.

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=””
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="" />
<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!

About Dougal Campbell

Dougal is a web developer, and a "Developer Emeritus" for the WordPress platform. When he's not coding PHP, Perl, CSS, JavaScript, or whatnot, he spends time with his wife, three children, a dog, and a cat in their Atlanta area home.
This entry was posted in Plugins, WordPress and tagged , , , . Bookmark the permalink.

106 Responses to My first WordPress plugin: HeadMeta

  1. Pingback: Blogging Pro - Blog News, Tools and Hacks » WordPress Plugin: HeadMeta

  2. Pingback: Weblog Tools Collection » geek ramblings >> My first WordPress plugin: HeadMeta

  3. Pingback: ABQ Group Weblog

  4. Pingback: Blogging Pro - Blog News, Tools and Hacks

  5. markku says:

    This is nice! Now we can have more high-ranking wp-powered sites in google. This should help provide exposure for those deserving.

  6. Danny Howard says:

    Uhmmm, if I download then bad bad things happen. Perhaps you could make a plain link to the code, or are you just showing off?


  7. Ah, that’s the PHP source auto-formatting. You could just cut-n-paste the text from your browser. But to make things easier, here’s a plaintext version:

  8. Orlando says:

    Message from WordPress Plugin Manager: “Fatal Error: Could not find plugin file in zip archive. Please contact developer and tell him to insure that his plugin is correctly named (should be: ‘headmeta.php’).”

  9. I just spent the last 30 minutes making a plugin that does exactly the same thing — DOH! I knew I should have searched first.
    Oh well, yours is smaller, so it looks like I just wasted my time 😐

  10. paul haine says:

    I think I’m missing something – I’ve installed the plugin with no problems, and then added a custom head_meta, but nothing extra at all appears in my <head> – no errors or anything, just…nothing.

  11. paul haine says:

    Ah – you need the wp_head line in your head for it to work.

  12. Barry Brown says:

    I just moved my webhosting yesterday and I’m looking for better ways of getting better results in the search engines. Your plug in looks great. I’m going to try it out, but I noticed you said in post 10 that I need the wp_head line in my head to make it work. Where does that actually go?

    I’m also wondering if I also can put metatags in any page on my site for better rankings. Just go to to see my daily deals site that I’m trying to develop all over again now that I’ve moved, but I like version 1.5!

    Thanks for any help you or anyone else can give,


  13. Dougal says:


    If you are using the default WordPress theme, it already has the wp_head() call in there for you (look at wp-content/themes/default/header.php, near the end). If you are creating your own theme (or using a third-party theme), just make sure that within the <head> section of the HTML, you have a line that says: <?php wp_head(); ?>

    This is what tells WordPress where to handle calls associated with the wp_head hook.

  14. sean says:

    Tries your plugin in my 1.5 version. But can’t see that it work. I uploaded the file and activated it, but no extra fields are shown. Do you have any idea why?

    Thanks in advance 🙂

  15. Timothy B says:

    Yeah I was wondering the same thing. Does this plugin work with 1.5?

  16. Dougal says:

    If you are using a custom theme, make sure that it is hooking into the wp_head API action. In the standard themes, this is handled in the header.php file, by including this code inside the html <head> section:

    <?php wp_head(); ?>

    Alternately, some themes might do it like this:

    <?php do_action('wp_head'); ?>

    If one of those code snippets doesn’t appear in your theme code, none of your plugins will be able to use the wp_head API call properly.

  17. Pingback: Taking Your Camera on the Road » Really Rocking with WordPress

  18. Pingback: Taking Your Camera on the Road » Really Rocking with WordPress

  19. Eli says:

    Hey there. I am having a problem with the plugin whenever it gets activated…it’s the same problem that happens with Xfish meta actually. I get this line of errors at the top of my page :Warning: Cannot modify header information – headers already sent by (output started at /usr/home/eliboat/public_html/wp-content/plugins/headmeta.php:1) in /usr/home/eliboat/public_html/wp-admin/admin.php on line 6

    Warning: Cannot modify header information – headers already sent by (output started at /usr/home/eliboat/public_html/wp-content/plugins/headmeta.php:1) in /usr/home/eliboat/public_html/wp-admin/admin.php on line 7

    Warning: Cannot modify header information – headers already sent by (output started at /usr/home/eliboat/public_html/wp-content/plugins/headmeta.php:1) in /usr/home/eliboat/public_html/wp-admin/admin.php on line 8

    Warning: Cannot modify header information – headers already sent by (output started at /usr/home/eliboat/public_html/wp-content/plugins/headmeta.php:1) in /usr/home/eliboat/public_html/wp-admin/admin.php on line 9

    Any idea what is going on here? ANy help is much appreciated. Thanks,

  20. Dougal says:

    90% of the time, errors of that type mean that you have some extraneous whitespace at the beginning or end of the plugin file. Make sure you don’t have any blank lines (or even spaces) before the opening “<?php” or after the ending “?>“.

  21. gsyi says:

    Nice idea 😉
    Thx for the plugin 🙂

  22. I really appreciate the great plugins available for WP users & I’m sure that this is a good one as well. But PLEASE, plugin authors, don’t write your instructions for people with your level of technical knowledge. Write them for the average WP user (like me) who may have some technical knowledge, but not as much as you.

    What I’m saying is that above there may be instructions on what to do to install this plugin but I couldn’t find them. The stuff that looks like it might be intructions doesn’t tell you what to do with the plugin code. Where do you install it?

    A little basic help would be much appreciated.

  23. After much trial & error I think I’ve got it working. A few suggestions: Dougal, you have introduced the white space in the file you link. There was space at the beginning & end of the file. ONce I removed that it installed.

    Also, it would be much easier if you offered the file as a notepad doc or zip so that we wouldn’t have to cut & paste text, create a file name, etc. I tried installing the file as .txt & then as .phps (the 2 suffixes you used above). Nothing worked until I used the .php suffix. This may seem elementary to some of you but it wasn’t to me.

  24. Pingback: geek ramblings » HeadMeta Update

  25. Nikki says:

    Dude, you rock! I tried a few plugins that did the meta tags but yours was the best and easiest to integrate with the other plugins I was using. I saved the formatting for keywords and description and was opening a notepad file to copy, paste and edit every single post. It got tiring so I quit a few days ago.

    This NEW adjustment is the bomb! Thank you, thank you, thank you — a million blessings upon you.

  26. Tore says:

    Seems to work great! Very useful. Not that I’m not content with my two regular readers.

    I am a bit in the dark still about meta tags. I haven’t added any to the main page of my weblog, such as a decription and some keywords. How will that play with this and the ? tag? I guess that’s probably a question for the WP support forum.

    Thank you for this plugin! Very useful!

  27. Pingback: Jens!Picek :: News-Blog » Blog Archive » Unser News-Blog und die Plugins

  28. Mike says:

    This works well on posts, but doesn’t appear to work on the pages feature of WordPress. Is this the case? If so is there anyway it can be used?

  29. if you have a key named “keyword” or “keywords”…

    Dougal: I guess I’m confused by a few things.

    I’m assuming that by the phrase above you mean to say that I can create a new custom field (which is WP’s term for what you’re talking about–I think) called “keyword.” You use the term “key” & I just wanted to make sure we were talking about the same thing.

    But how would this be diff. than the old method by which you invoked a custom field call meta_tag? Or are you saying that you can have a series of custom fields for posts which share the same general subject & whose keywords might be similar?

    I hope you’ll help clear this cloud over my head…

  30. Dougal says:

    Mike: I’ll release an update soon that works on pages.

    Richard: Sorry about the confusing terminology. Yes, I’m talking about the post custom fields, which have a “key” and an associated “value” for the key. The using the word “keywords” as the key is a shortcut for a “keywords” meta tag. The following pairs of keys and values would be equivalent:

    KEY: head_meta
    VALUE: name=”keywords” content=”entertainment,movies”

    KEY: keywords
    VALUE: entertainment,movies

    These will both generate the exact same <meta> tag in the header. It’s just that the second form is easier to use.

  31. Blogmail says:

    I am testing the instalation, I loved this WordPress.
    It seens to be easy to manage, I hope so.

  32. I am looking for plugin to have but button of XML, Atom, Yahoo, etc. Exist plugin?


  33. harry says:

    I still get the same error message.”headers already sent by (output
    started at …catalog/includes/application_top.php:509)”

  34. ylek says:

    hi dougal, i’m try this plugin on my trial site and it’s a miracle 😀 i’m looking for one to do that for a long time
    you make an excellent plugin. it’s easy to install and run (i’m don’t know nothing about php)

    i’l like make only a question. this plugin works perfect on posts but doesn’t works on the pages, why? it’s there anything can i do to resolve that?

    thanks again and sorry for my bad english (i’m try writte, but i don’t know your language well)

  35. Just downoaded this vital plugin and installed it. But I get the error which says: “parse error, unexpected T_RETURN in…”
    Any solution would be appreciated.
    Thanks in advance

  36. kike says:

    Warning: array_merge() [function.array-merge]: Argument #1 is not an array in XX/wp-content/plugins/headmeta.php on line 52

    I’m using PHP5, and that’s the warning that appears into one page.
    Any solutions?

  37. kike says:

    I’ve changed this
    $keywords = array_merge($post_meta_cache[$id][‘keyword’],$post_meta_cache[$id][‘keywords’]);
    $keywords = array_merge((array)$post_meta_cache[$id][‘keyword’],$post_meta_cache[$id][‘keywords’]);

    and the warning has disappeared

  38. Pingback: SonicChicken weblog » Blog Archive » New website for my wife’s business

  39. The Finite Monkey says:

    I tried installing the headmeta plugin, and like Shekharpoorna earlier in the comments (Sep 29), I received the following error when I tried to activate it:

    “Parse error: parse error, unexpected T_RETURN in /path-to-my-wordpress/plugins/headmeta.php on line 39”

    Line 39 reads:

    if (!(is_single() || is_page()) return;

    If I can get the basic plugin to work, my next order of business will be to attempt tweaking it to automatically do with a ‘description’ tag what you already have it doing for ‘keyword’ or ‘keywords’. Thanks for the plug-in.

  40. Dougal says:

    Well, I’ll be. Thanks for posting that line, I noticed what the problem was. It was missing a paren at the end. It should read:

    if ( !(is_single() || is_page() ) ) return;

    I added extra spacing for better readability. Notice that there are three closing parens at the end, not two.

    Sorry about the mistake. I’ve updated the downloadable versions.

  41. Serge says:

    good stuff, now i will raise my botd-isms

  42. Paul says:

    If I use this plugin I can do keywords, however, it it possible to also use this
    plugin to generate –

    Description, Abstract, Author, Robots, and Distribution

    Meta Tags?


  43. Lana says:

    Greetings 🙂

    Quick Question.

    I just installed this, however when I view the sorce of my page, I don’t see the keywords.Did I do something wrong?

    Thanks 🙂

  44. Dougal says:

    Lana, make sure that your blog theme is calling the wp_head action. There’s a previous comment with more details.

  45. Valerie says:

    Hello -I am getting the same error message as “kike”:
    Warning: array_merge() [function.array-merge]: Argument #2 is not an array in \\nas-001\winspace002\\www\wp-content\plugins\headmeta.php on line 53

    I applied his fix but still no luck 🙁

  46. Valerie says:

    still wondering if anyone knows how to get this plugin working when you get this warning:

    Warning: array_merge() [function.array-merge]: Argument #2 is not an array in \\nas-001\winspace002\\www\wp-content\plugins\headmeta.php on line 53

    tried -sugested “fix” -no luck.

  47. Pingback: Blogger Tipps und Hilfe » Archiv » Metatags in Wordpress einbauen

  48. Pingback: SonicChicken weblog » Blog Archive » CJD on Google Earth

  49. Pingback: a.css, esbudellant estàndards » Metadades personalitzades pel WordPress

  50. Pingback: Combining Bunny Technorati Tags and Head Meta Plugin | Webmaster Resources Information

  51. mr guy says:

    Parse error: parse error, unexpected T_RETURN

    that’s the error i get for line 21

    line 21 reads:
    $j == FALSE? return substr($string, $i++);

  52. Dougal says:

    There’s not a line like that in the HeadMeta plugin. Double check the error message to see what file it’s coming from.

  53. Mitch says:

    I must have something set wrong. The only thing that’s showing up on my Google ads are concerning blogs, and that’s not what it used to show before I just had to recover everything. Any ideas on what I might have missed?

  54. Richard says:

    Problem: meta tags not showing in of page.

    I have previously successfully installed the Headmeta plugin in WP version 2.0.2, but I’m not having any success with WP version 2.0.3

    I’m using the same theme (Gila)
    header.php has the line in it.

    I’m using the key head_meta and values name=”keywords” content=”CMS” and name=”description” content=”some description”

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

  55. Richard says:

    Referring to my previous comment, part of which didn’t show up I’ll try again.

    The header.php file has the line: <?php wp_head(); ?>

  56. Richard says:

    It’s all working fine now. I think it may have had something to do with the way I was viewing the page having added the values.

  57. Robert Howe says:

    Hi: I am not getting any meta data and having written some simple de-bugging lines, it looks as if $metavals is not being populated. Running on WP2.0.2 … I’m not a PHP developer, but I can follow what the code is doing and making small tweaks. Also, where in a normal instalation, would I find the error logs people are referring to please?

  58. Robert says:

    Thanks for plugin! Dougal

    It is my understanding that there is no problem with having (2) meta tags for keywords (or description) as long there is no duplicate content. You can keep a more general, permanent keyword meta tag in your header, then use this plug-in to further refine the tagging. The search engines will combine the content as long as the length limit is not exceeded. You can repeat a ‘word’ but not a ‘keyword’.

    Example – stays in the header. Then, use the HeadMeta plugin to generate a second tag – . Both will appear in header section of the page source.


  59. Robert says:

    Whoops! Forgot the code tags-

    It is my understanding that there is no problem with having (2) meta tags for keywords (or description) as long there is no duplicate content. You can keep a more general, permanent keyword meta tag in your header, then use this plug-in to further refine the tagging. The search engines will combine the content as long as the length limit is not exceeded. You can repeat a ‘word’ but not a ‘keyword’.

    Example –
    stays in the header. Then, use the HeadMeta plugin to generate a second tag . Both will appear in header section of the page source.

    Great plug-in!

  60. dsl presse says:

    i have problem wih my blog under, can somebody help me or say whow i can deactivate that the startsite (homepage) are so long, is t better to have a short homepage (first site)?
    Whis metatags i dont understand whow the plugin is to install and if it possible to have diverent tags for all sites, plese mail me.

  61. Ricardo says:

    This script not work with WordPress 2.0

  62. Dougal says:

    It’s working fine here, under WordPress 2.0.4.

    If you view source on this page, you can find the following three tags generated by the plugin:

    <meta name="plugin" content="HeadMeta" />
    <link rel="source" href="" />
    <meta name='keywords' content='wordpress, plugin, metadata, headmeta' />
  63. Pingback: The Online Developer

  64. Pingback: Wordpress meta-tags - Platinax Business Forums

  65. Dan says:

    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.

  66. Little Money says:

    Cheers for making this plugin! I find it very useful! 🙂


  67. ovidiu says:

    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?

  68. Dougal says:

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

  69. ovidiu says:

    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.

  70. Gerald says:

    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 for detailled information.

  71. Dougal says:

    Now WordPress 2.1 compatible.

  72. Matthew says:

    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!

  73. hotlogo says:

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

  74. Pingback: Update for HeadMeta plugin for WordPress 2.1 - Recommended Web Tools

  75. Pingback: Brandon Buttars » WordPress Meta Tags Plugin

  76. Joe says:

    It doesn work!!! :-(((

  77. Pingback: Wordpress META Plugin List | ngadutrafik2007

  78. Dougal says:

    It doesn work!!! :-(((

    Can you be a little more vague, please? 🙂

  79. Pingback: UmatBlog » WordPress very exPRESSive

  80. Pingback: Update for HeadMeta plugin for WordPress 2.1 | READER: Webmaster

  81. Pingback: WP Plugins DB » Plugin Details » HeadMeta

  82. manele says:

    thx for this very very useful plugin!

  83. Pingback: Work at Home Blog » Top 5 Wordpress Plugins for Bringing Traffic

  84. Matthias Pospiech says:

    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?


  85. dieta says:

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

  86. Pingback: HeadMeta_Wordpress plugin

  87. Pingback: geek ramblings » WordPress 2.5 Beta

  88. Pingback: Weblog Tools Collection » Blog Archive » New AB Meta Markup Tools for WordPress

  89. Pingback: ThinkLite » Wine, Film and Books: Adaptive Blue Offers Open Format to Make the Web Smarter

  90. Firepow says:

    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.

  91. Pingback: Wine, Film and Books: Adaptive Blue Offers Open Format to Make the Web Smarter « Mayank’s Weblog

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

  93. 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 …

  94. Richard says:

    Nice plugin and easy to use 🙂

  95. Josh says:

    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.

  96. Goran Web says:

    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.

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

  98. Goran Web says:

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

  99. Montxo says:

    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 says:

      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.

      • Montxo says:

        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.

  100. Nick Aviles says:

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

  101. This script not work with WordPress 2.0

  102. Pingback: Photography and Travel Taking Your Camera on the Road – Really Rocking with WordPress

Leave a Reply

%d bloggers like this: