Using WordPress Comment Meta

UPDATE 8/18/2011: As of WordPress 3.2 there still isn’t any native way to view comment meta in the admin, but I have released a plugin that allows you view the data (no editing/adding yet). Download the plugin here.

One of the features in WordPress 2.9 that I was the most excited about was comment meta. This allows us to use the WordPress comment fields much more robustly and do some really cool stuff. Unfortunately there’s no documentation added on the WP codex and I found very little on the web. So after doing a lot of scrounging and testing, I’m submitting this guide for how I added some custom user content to each user comment.

In this tutuorial, we’ll be editing your comment template and functions.php. A healthy level of programming experience is required. I’d also like to note that there’s probably many ways that this code could be made more elegant. My goal is just to have some working code that gets the job done.

Step 1
First we need to solicit user input. For my purposes, I was trying to attach a custom user id generated elsewhere to each comment. This could be a user-entered field, though. Add this code to your comment submission form:

<input type="hidden" name="my_user_id" value="<?php echo $myid; ?>" />

Step 2
Next we need to add this field as a piece of MetaData attached to the new comment. We use the WordPress hook system to accomplish this. Add this to your functions.php file:

add_action ('comment_post', 'add_meta_settings', 1);
function add_meta_settings($comment_id) {
	add_comment_meta($comment_id, 'my_user_id', $_POST['my_user_id'], true);

Step 3
For the final step, we need to read our new data along with each comment. I’m using the callback method to display my comments. In your callback function add the following code:

$GLOBALS['comment'] = $comment;
$my_user_id = get_comment_meta(get_comment_ID(),"my_user_id", true);
echo $my_user_id;

I hope this will help get you pointed in the proper direction. Enjoy…

32 Responses to “Using WordPress Comment Meta”

  1. Thanks for the code! I am working on a drop down menu to plug into the comments meta table and I think your method will at least get me in the right direction.

  2. Thank you thank thank you so much for this, this will go along way to help with a project that I am working. I have been really pushing WP to its limits as a CMS with custom meta boxes for custom post types, pages, profiles and even categories and this will go along way to make the comments bend to my will with new comment types without resorting to extra tables and SQL queries and more hacks. Thank you again!

  3. get_comment_ID() returns empty. I assume our comment templates are not the same.

    I’m using the “classic” theme in wp 2.9.2

    Also, database fields are created but value not saved. Using the comment ID value instead works. (I used the value a comment ID value I entered by hand in the DB)

    What can I try now?

  4. This isn’t quite working for me, so I’m fiddling and trying to understand it.

    Since you are updating comments, why do you refer to $post_id and not $comment_id?

    Also, when would one use add_comment_meta() vs update_comment_meta()?

    I assume that either function works on existing database rows, so they seem interchangeable.

    this confuses me:
    as you are hard coding a variable, which you assign to $post_id
    I would think it should be $comment_id, and not hard coded. Can you explain?

  5. barring that I don’t get why $post_id is referenced instead of $comment_id

    I’ve found a very similar tutorial here:

    I implemented theirs verbatim, and still no go. I’m on 2.9.2. What could I be missing? what about adding the fields in mysql? Neither tutorial mentions that, but I assume I must add the rows manually, yes?

  6. @Sergio: I’m not sure why the get_comment_id() is returning empty for you. If you can send me your code at web at sparkweb dot net I will take a quick look at it and see if anything jumps out at me.

    @Dan: I’m not actually updating comments. I’m adding a new meta record that is related to the original comment. If you were making a change to a piece of comment meta that you had already added you would update, otherwise add. On the add_action, that is just setting the hook. The actual comment_id gets passed in with the comment_custom_fields() function. Actually $post_id is confusing because it should actually be $comment_id. The result is the same because it is just a variable being passed in, but it is confusing. Sorry.

    But you don’t need to add the fields in manually… this function does that. You are making sure that the form has a field called “twitter”, right?

  7. I got a lot farther with this. I now see that the data is being stored in wp_commentmeta. Phew! Now I’m trying to get the new metadata to display along with the comments. I’m apologizing in advance for my complete lack of knowledge with php. here are the relevant snips from my custom_comments function….
    $GLOBALS[‘comment’] = $comment;
    $townArray = get_comment_meta(get_comment_ID(),”town”);
    $town = $townArray[0];
    $celebrationArray = get_comment_meta(get_comment_ID(),”celebration”);
    $celebration = $celebrationArray[0];

    … and …

  8. never mind, I figured it out!

  9. thank you very much .
    I use your function to make my commenters have a chance to choose that if they want to receive an email notification or not when their comment be relied. I add a comment meta to record if they checkd the option. thanks !

  10. You can return the comment meta as a string (as opposed to an array) by giving it a third argument. The function works the same way as get_post_meta().

    get_comment_meta(get_comment_ID(), ”celebration”, true);

    Thanks for the post!

  11. Hi,

    Thanks for this – just what I was looking for.

    I have been trawling the web for a solution to allow users to edit the extra comment meta fields in the “edit comments” admin panel.

    Does anyone know how I could achieve this by adding a filter to my functions.php?


  12. Hi, great info!

    One question though. I am trying to use the ajax function that the “WP Thread Comment” offers on comments, but when using ajax the comment doens’t save the comment-metadata…Any ideia which direction to go?

    Thank you 🙂

  13. Thanks for the code.How can I edit that extra field value from edit comment in admin?

  14. @parmajeet I don’t know. I’m sure there’s an admin hook somewhere, but I haven’t been able to find it. Let me know if you come up with something.

  15. […] source Запись опубликована в рубрике Без рубрики с метками Soapbox Dave. Добавьте в закладки постоянную ссылку. ← Привет, мир! […]

  16. […] html5 3.0 and use that theme’s comment system. I have followed custom_form tutorials by Otto, Soapbox Dave, forlogos, and Deluxe Blog […]

  17. wow! really very helpful! Thanks

  18. How can I get all comments with specific meta value?

  19. I think you’d need to roll your own query with $wpdb. There’s no native functions that I know of that would accomplish this.

  20. Can you explain in more detail how to do this with a text field? I am having problems using it the way I want.


  21. I’m not sure exactly what I can add. Just make sure that you do input type=”text” rather that type=”hidden” and update your hooks accordingly. Send me your sample code at web -at— sparkweb (dot) net and I’ll take a look.

  22. thanks 🙂

  23. yes, just wanted to say thanks for posting this – it was just what i was looking for, and totally got the job done :

  24. Thanks, this is just the solution I was looking for to add an extra field for users that are already logged in.

    The only problem I am having is making the extra field required, for some reason it just keeps bypassing the field when not filled out. Any Ideas?


  25. Luke, I’m not sure on this. You might need to write some jQuery to intercept the submit process.

  26. How can I make this work in WordPress 3.3?

  27. Karim, is it not working in 3.3? This method should work fine in 3.3 as well.

  28. Where would I put the code in step 1? Because now, the comment form is replaced with the function comment_form(); in comments.php.

  29. Gotcha. It looks like comment_form() can be adjusted through the arguments passed through to that form. More details at: Or you could use a manual comment form from a different theme.

  30. This is terrific, but I just noticed that when I use the Askimet plugin for comment spam, it stops working. Instead of the field I added in the comment form being displayed, I see the value for “akismet_result” in the comment meta area in the comment. Any way to fix this? I have had to temporarily disable the Askimet plugin.

  31. I just updated to version 1.1 at with some changes. Can you please update and try again to see if this helps?

  32. Thanks, David. That fixed it! I now see all of the meta data in the meta comment area.

Leave a Reply