Nate Weiner

This is an old archived post from my former blog The Idea Shower. It's where I cataloged my product explorations and releases, one of which ultimately became Pocket.

This post was published back in 2007. It may not function as originally intended or may be missing images.

Wordpress Plugin: Extra Comment Fields

September 29, 2007

Overview

This Wordpress plugin allows you to add additional fields for users to complete when submitting a comment. The extra data will be saved in the wp database and can be retrieved when showing a post's list of comments.

The extra comment variables will show up in the list of comments in your Wordpress admin and are editable through the standard admin comment edit page.

This plugin requires a basic knowledge of HTML to modify your comment form.

Jump to Download

Example of Use

For this example, we are going to add a field asking for a users age to the comments form.

1. First, add the field to your comments form.

Typically your comments form is in your theme folder and is comments.php Most likely: (/wp-content/themes/YOURTHEME/comments.php)

Enter the HTML of your new field into the desired spot. To add a textfield for the user to enter their age into, you would add the following to your form. I'll add mine right below the url textfield.

<p><input type="text" name="age" id="age" size="22" />
<label for="url"><small>Website</small></label></p>

Screenshot of Adding Field

2. Next, we need to add the field-variable to Wordpress.

Inside your Wordpress admin, click the 'Settings' menu. Then click the 'Extra Comment Fields' menu.

Next, enter the name of the field. This has to be the exact name attribute you gave to the field you added to your comments form. For example, I added this field to my form:

<p><input type="text" name="age" id="age" size="22" />
<label for="url"><small>Website</small></label></p>
The input's name attribute is 'age', so the variable you would enter for this field is 'age'.

Note: This can only contain letters, numbers, and/or dashes.

3. Last we want to make use of the data somehow when showing comments.

Your extra fields will be in the $comments variable retrieved by Wordpress. The extra fields you added will have a prefix of 'extra_'. So to access the data for our 'age' field, we use $comment->extra_age inside the comments loop.

So, for example, if I modify the default theme's comment.php file, I just insert one line to display the extra comment information (highlighted in bold).

<ol class="commentlist">

<?php foreach ($comments as $comment) : ?>

<li <?php echo $oddcomment; ?>id="comment-<?php comment_ID() ?>"> <?php echo get_avatar( $comment, 32 ); ?> <cite><?php comment_author_link() ?></cite> Says: <?php if ($comment->comment_approved == '0') : ?> <em>Your comment is awaiting moderation.</em> <?php endif; ?> <br />

<small class="commentmetadata"><a href="#comment-<?php comment_ID() ?>" title=""><?php comment_date('F jS, Y') ?> at <?php comment_time() ?></a> <?php edit_comment_link('edit',' ',''); ?></small>

<?php comment_text() ?> User's Age is: <?php print $comment->extra_age; ?>

</li>

<?php /* Changes every other comment to a different class */ $oddcomment = ( empty( $oddcomment ) ) ? 'class="alt" ' : ''; ?>

<?php endforeach; /* end for each comment */ ?>

</ol>

Another Example Posted on Problogger

How I added the Twitter ID field to comments on Twitip.com

Version History

1.2 - June 23rd, 2008
  • Corrected behavior when deleting/changing status of comment
1.1 Alpha - May 20th. 2008
  • Fixed blank comment content field
1.0 Beta - May 4th, 2008
  • Updated for WP 2.5
  • Added ability to edit comments
  • Added ability to view comments in admin
  • Extra Comments data deleted when comment is deleted
  • Fixed database table prefix
0.2 Alpha - Sept. 2007
  • Initial Release

Download

NOTE: I am no longer developing this plugin.  If you'd like to continue it, please consider it open source.  If you release a new version, update users in the comments below.  Thanks

Download the Extra Comment Fields Wordpress Plugin version 1.2 Beta (For Wordpress 2.5+)

Download the Extra Comment Fields Wordpress Plugin version 0.2 Alpha (Only for Wordpress 2.0 - 2.3)

To install, unzip the files into your /wp- content/plugins/ folder. Then activate it under the Plugin menu in your Wordpress admin.

Important Note When Upgrading from 0.2 to 1

If your Wordpress database tables do not start with 'wp_', you will likely need to rename the wp_comments_extra table in your database to match the other table's prefix. This is a result of the first version incorrectly assuming the prefix across all installations.

For example, if you have have tables named called prefix_posts, prefix_comments, prefix_options, etc then you should rename the wp_comments_extra table to prefix_comments_extra.