PMG Digital Made for Humans

Adding Extra Fields to WordPress Comments

5 MINUTE READ | September 21, 2011

Adding Extra Fields to WordPress Comments

Author's headshot

Christopher Davis

Christopher Davis has written this article. More details coming soon.

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.

A few versions back WordPress standardized the comments form for theme authors with the creatively named 

 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.

        <label for=“pmg_comment_title”><?php_e(‘Title’);?></label>
        <input type=“text”name=“pmg_comment_title”id=“pmg_comment_title”/>

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.

    <style type=”text/css”>
        #respond .comment-form-title {
        #respond .comment-form-title label {

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 

 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.
    add_meta_box(‘pmg-comment-title’,__(‘Comment Title’),‘pmg_comment_tut_meta_box_cb’,‘comment’,‘normal’,‘high’);
        <label for=“pmg_comment_title”><?php_e(‘Comment Title’);?></label>
        <input type=“text”name=“pmg_comment_title”value=“<?phpechoesc_attr($title);?>“class=“widefat”/>

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 

 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 
, 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 
 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.

<?php add_action(‘edit_comment’,‘pmg_comment_tut_edit_comment’); functionpmg_comment_tut_edit_comment($comment_id) { if(!isset($_POST[‘pmg_comment_update’])||!wp_verify_nonce($_POST[‘pmg_comment_update’],‘pmg_comment_update’))return; if(isset($_POST[‘pmg_comment_title’])) update_comment_meta($comment_id,‘pmg_comment_title’,esc_attr($_POST[‘pmg_comment_title’])); }

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 

 to fetch our title somewhere in the custom callback specified for 
. 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 



Stay in touch

Bringing news to you

Subscribe to our newsletter

By clicking and subscribing, you agree to our Terms of Service and Privacy Policy

The entire code for this article is available as a plugin.

Related Content

thumbnail image

AlliPMG CultureCampaigns & Client WorkCompany NewsDigital MarketingData & Technology

PMG Innovation Challenge Inspires New Alli Technology Solutions

4 MINUTES READ | November 2, 2021

thumbnail image

Applying Function Options to Domain Entities in Go

11 MINUTES READ | October 21, 2019

thumbnail image

My Experience Teaching Through Jupyter Notebooks

4 MINUTES READ | September 21, 2019

thumbnail image

Working with an Automation Mindset

5 MINUTES READ | August 22, 2019

thumbnail image

3 Tips for Showing Value in the Tech You Build

5 MINUTES READ | April 24, 2019

thumbnail image

Testing React

13 MINUTES READ | March 12, 2019

thumbnail image

A Beginner’s Experience with Terraform

4 MINUTES READ | December 20, 2018