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:

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!

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:
add_action(‘comment_post’,’comment_custom_fields’,1);
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?

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?

@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?

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];

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 !

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?

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.

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.