Customising Wordpress Comments
I recently started working on a review site based on Wordpress. It was clear that the easiest way to achieve review functionality was to use the Wordpress comment system. Basically, items were added to the site as posts, and post comments would serve as reviews for the items.
Having made the decision to use the comment system, I needed to then add some extra fields to the comment form, which would accept pieces of information that were part of the reviews. I searched around briefly for some information on the web regarding how to modify the Wordpress comment system, but could not find much, hence I have written the following short account of how to add custom field to the Wordpress comment system.
To begin with, here are the 3 files that you will need to edit:
- /wp-content/themes/[Your Theme]/comments.php (i.e. your theme’s comments file)
- /wp-includes/comment.php
- /wp-comments-post.php
Now, say that we wanted to add an extra field to the comment form, a simple text box into which the user inputted their favourite colour. We would put something like the following line into the section of your theme’s comments.php where the form fields are located.
<blockquote><code><p><input type="text" name="favcolour" id="favcolour" value="" size="22" />
<label for="favcolour"><small>Favourite Colour</small></label></p></code></blockquote>
The next thing which requires modification is the wp-comments-post.php file. Around line 38 the form data is assigned to variables. After the line
$comment_content = trim($_POST['comment']);
we will need to assign our new form data to a variable with a line like this:
$comment_favcolour = $_POST['favcolour'];
Once that is done, it will need to be added to the final data array. Look for a line like this near the end of the file:
$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID');
and add to it the new ‘favcolour’ variable to get:
$commentdata = compact('comment_post_ID', 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_content', 'comment_type', 'user_ID', 'comment_favcolour');
Once that is done, it is time to edit comment.php. Around line 635 there will be a line where the data is inserted into the database; it will start off like this:
$result = $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->comments
(comment_post_ID, comment_author, comment_author_email, comment_auth ...
Once the ‘favcolour’ information is added to this line, it will look something like this:
$result = $wpdb->query( $wpdb->prepare("INSERT INTO $wpdb->comments
(comment_post_ID, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, comment_parent, user_id, comment_favcolour)
VALUES (%d, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %d, %d, %d, %s, %s)",
$comment_post_ID, $comment_author, $comment_author_email, $comment_author_url, $comment_author_IP, $comment_date, $comment_date_gmt, $comment_content, $comment_approved, $comment_agent, $comment_type, $comment_parent, $user_id, $comment_favcolour);
Tags: Wordpress, Wordpress Comments, Wordpress Customisation

















































