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…
Filed under: Internet on February 11th, 2010 | 29 Comments »


just finished up my first WordPress plugin. It’s a little app that uses Google’s caching servers to more reliably post your tweets in the sidebar. You can see how it works on the sidebar of this blog.
Five Guys Burgers and Fries
There is a lot of buzz about Ron Paul on the Net and around town. In fact a lot of my friends are Ron Paul supporters. I had a general idea that I wasn’t crazy about his stance on some issues, but I wasn’t very solid on it so I decided to research his views and really make up my mind on where I stood on Congressman Ron Paul.
