WordPress comes built in with a full featured, robust commenting system. If you write a blog, you probably want to interact with your readers. Comments give you a way to do that.
But WP’s built in comments might come in handy for other purposes. For instance, if you’re running a small ecommerce store on WordPress, you could use the commenting system to do customer reviews. You could also turn the comments into a sort of forum.
This tutorial is not going to be about specific usage, but, rather, a general how to for adding fields to comments on both the front end and back end of WordPress. We’ll be using the default Twenty Eleven theme here.
1. Adding Front End Form Fields
A few versions back WordPress standardized the comments form for theme authors with the creatively named comment_form
function. Because it’s part of the core, the function comes loaded with hooks for plugin/theme authors to use to customize the form’s contents.
We’re going to use a few of those hooks: comment_form_logged_in_after
and comment_form_after_fields
. They will add something after the “You’re logged in as…” message or the name, email and website fields respectively.
We’ll just call add_action
for each of our hooks and in our hooked function spit out form code we want.
Because we’re using Twenty Eleven, let’s hook into wp_head
and spit out some CSS to prettify our form field. This is copied straight from the Twenty Eleven stylesheet adapted to our selector.
2. Adding the Admin Form Field
The comments editing screen in the WordPress admin area is just like any other editing screen: you can add meta boxes to display and save additional data. You can check out this tutorial for a general overview of adding meta boxes.
First we’ll need to add our meta box with a function hooked into add_meta_boxes_comment
and a call to add_meta_box
with comment as our page and normal as our context. add_meta_box
‘s third argument is a callback (the second function below) which takes care of outputting all the meta box’s content. We’ll also include a nonce field that we’ll use for some data validation purposes later on.
{
$title = get_comment_meta( $comment->comment_ID, ‘pmg_comment_title’, true );
wp_nonce_field( ‘pmg_comment_update’, ‘pmg_comment_update’, false );
?>
<p>
<label for=”pmg_comment_title”><?php _e( ‘Comment Title’ ); ?></label>
<input type=”text” name=”pmg_comment_title” value=”<?php echo esc_attr( $title ); ?>” class=”widefat” />
</p>
<?php
}
3. Saving the Title
Next up, we need to actually take care of saving the title for our comment. We’ll need to do this in two places: the front end form and the admin area meta box.
When WordPress outputs a comment form, it does so with a file called wp-comments-post.php
as the form action. The first step, when trying to add functionality to some built in WP features is to go the file that does the work and start looking for do_action
and apply_filters
calls (hooks). There are none in wp-comments-post.php
, so you have to go a little deeper. The file calls a function called wp_new_comment
, which takes care of inserting the comment into the database. It also includes a hook called comment_post
we’ll use.
Insider of the function hooked into comment_post
, you’ll have access to the global PHP $_POST
array which will contain our title. First we’ll check and make sure its there, then use update_comment_meta
to save our title.
For the admin area, we need to do a bit more work. First we’ll verify our nonce, then check and make sure our title is set and update it if it is.
4. Sharing Comment Titles with the World
Now it’s time to actual show the comment titles on the front end. There are two ways one could do this:
1. Include a call to get_comment_meta
to fetch our title somewhere in the custom callback specified for wp_list_comments
. This would be the most flexible option as it would give complete control of the layout.
2. Hook into the comment_text
filter, grab the comment title with get_comment_meta
, then add it to the comment text before sending it out.
We’ll use the second option in this case. We’ll hook into comment_text
, first checking to see if we’re in the admin area. If we are, return the content straight away. Next we’ll try to get our comment title. If it’s there, we’ll add it to the text. Then we send the comment text back out.
WordPress uses its own plugin API to do several things within the core. Namely, it adds certain filters to your post content text and comment text. We need to call our comment_text
filter way late to ensure it doesn’t step on any of those built in filters toes (or, rather, that those default filters don’t screw up our formatting). This is done with the third argument of add_filter
. The higher the number, the later, the function is called. If you want to see how the WP core uses its own hook system, check out wp-includes/default-filters.php
.
The End Result: Comments with Titles
The entire code for this article is available as a plugin.