Add a Field to Your Wordpress Comments
I recently embarked on some PHP and MySQL programming this past week to create part of my site SprintRants.com. I am using Wordpress’s comments template to allow people to leave rants on a single page set up to do just that. The only trouble was that I wanted to enable my users of the page to title their posts to allow visitors to the site an easier time navigating what each rant was about.
To do this I need to figure out a way to add a field to my comments form (easy) and also to my MySQL database (hard). I have no experience up to this point with MySQL and very little with php so I knew it wasn’t going to be easy. I searched around the Wordpress forums as well as Googled the heck out of it. After searching and searching I found a thread over at the Wordpress Support page that pointed me in the right direction to add a field to MySQl and my comments template. Coldforged over there had a great post which started me going. It wasn’t super detailed but let me know which php files to mess with. From there I spent a few long hours reading through the existing code in the files and trying to determine what I needed to add.
After about 5 hours I finally figured it out and was able to add a title to not only my add comments template but also to the plugin that I am using provided by Raoul called Simple Recent Comments.
Within this post I tried to detail what I did within each PHP file as best I could. I hope it helps you out and I hope this is really easy for someone who hasn’t had any php or MySQL programming in their past.
WARNING: Just remember to save backups to all the files you have been messing with.
Adding your field to MySQL database
This is probably going to be different for everyone’s hosting company. I personally use NetFirms and when I go to my database manager in site tools, choose the correct database (the number of which can be found in the wp-config.php file in the root folder (by root folder I mean the first folder of your blog that has other files and folders, like wp-admin in it) of the wordpress blog folder you are adjusting the comments for), I have an option called phpMy Admin. Clicking on this takes me to the MySQL administration page where I can make adjustments to the database. I get to the administration portion for wp_comments and find where it will allow me to add a new field. Add your field and add the correct labels and data type, just remember that you need to write down the name with the capitalization correct to use in the php files we are going to adjust.
I added the field comment_title to the database and that will be used as an example for the rest of this tutorial.
Add code to wp-comments-post.php
wp-comments-post.php is in the root folder of your Wordpress blog folder. I believe this is the file that is called to post comments on your blog retrieving the info from your MySQL database. What you need to do is add some code to the following lines where I have added the code in italics. I also noted what the line was but it might be different for you.
Starts on line 19:
$comment_author = trim($_POST['author']);
$comment_author_email = trim($_POST['email']);
$comment_author_url = trim($_POST['url']);
$comment_content = trim($_POST['comment']);
$comment_title = trim($_POST['title']);
Starts on line 48:
$commentdata = compact(’comment_post_ID’, ‘comment_author’, ‘comment_author_email’, ‘comment_author_url’, ‘comment_title’, ‘comment_content’, ‘comment_type’, ‘user_ID’);
Next add code to functions-post.php
I next, following coldforged suggestion path added some code to functions-post.php which was in the wp-includes folder. Again I’m not too sure what this function does but I’m guessing it posts things to the MySQL database? Whatever it does, additions are noted the same as above.
Starts on line 455:
function wp_new_comment( $commentdata, $spam = false ) {
global $wpdb;
$commentdata = apply_filters(’preprocess_comment’, $commentdata);
extract($commentdata);
$comment_post_ID = (int) $comment_post_ID;
$user_id = apply_filters(’pre_user_id’, $user_ID);
$author = apply_filters(’pre_comment_author_name’, $comment_author);
$email = apply_filters(’pre_comment_author_email’, $comment_author_email);
$url = apply_filters(’pre_comment_author_url’, $comment_author_url);
$comment = apply_filters(’pre_comment_content’, $comment_content);
$comment = apply_filters(’post_comment_text’, $comment); // Deprecated
$comment = apply_filters(’comment_content_presave’, $comment); // Deprecated
$title = apply_filters(’pre_comment_title’, $comment_title);
Starts on line 514:
Note that the location of these two additions must be the same in the two lists. I put mine second. The value has to match the place holder in the database or else you’ll have some jumpled information. Notice it inserts the value from $title into the MySQL database spot your created at comment_title in the first step.
$result = $wpdb->query(”INSERT INTO $wpdb->comments
(comment_post_ID, comment_title, comment_author, comment_author_email, comment_author_url, comment_author_IP, comment_date, comment_date_gmt, comment_content, comment_approved, comment_agent, comment_type, user_id)
VALUES
(’$comment_post_ID’, ‘$title’, ‘$author’, ‘$email’, ‘$url’, ‘$user_ip’, ‘$now’, ‘$now_gmt’, ‘$comment’, ‘$approved’, ‘$user_agent’, ‘$comment_type’, ‘$user_id’) “);
Now mess with comment-functions.php
This was the one that took me the most time to figure our where to add the code. I finally had a gadunken and figured out that I needed to add a function. Cool:) The comment-functions.php is in the wp-includes file as well. This file I believe is where you draw the comments from when you post the comments?
Starts on line, well I think you can add this anywhere past line 133, just add them in the middle of the already there “get_comment” functions. I am not sure if this function format will work for every field you want to add to the comments section as some of the “get” functions are different. Play around with the format until you get one that works for you if this format doesn’t.
function get_comment_title() {
global $comment;
$title= $comment->comment_title;
return apply_filters(’get_comment_title’, $title);
}
function comment_title() {
$title = apply_filters(’comment_title’, get_comment_title() );
echo $title;
}
Now add the appropriate code to your Comments.php file in your themes templates
Now I can get to my templates through my regular wordpress login and not have to deal with an ftp client. I assume you can as well. The final things you have to add are this.
If you want the new field to post within the comments display after the comment has been written and within wherever you post comments, be it a page or post you have to add code to the foreach comments loop. I added it as the title but just through in the code wherever to get the effect you want.
< ?php foreach ($comments as $comment) : ?>
<li id=”comment-< ?php comment_ID() ?>“>
<h3>< ?php comment_title() ?></h3>
< ?php comment_text() ?>
<cite>< ?php comment_type(__(’Comment’), __(’Trackback’), __(’Pingback’)); ?> < ?php _e(’by’); ?> < ?php comment_author_link() ?> — < ?php comment_date() ?> @ <a href=”#comment-<?php comment_ID() ?>”>< ?php comment_time() ?></a> < ?php edit_comment_link(__(”Edit This”), ‘ |’); ?></cite>
</li>
You need to get the input from the user which is done in the form section like this. You’ll see code that looks just like this at the bottom of the comments.php option, it’s the code to display the form for comments on all of your pages.
<p><input type=”text” name=”title” id=”title” value=”<?php echo $comment_title; ?/>” size=”22″ tabindex=”3″ />
<label for=”title”><small>Title or Subject for your Rant (appreciated)</small></label></p>
You’re DONE!
There you have it. You’re done with adding a field to your comments section for later display. Just remember that you are taking the variable (example) $title and placing it in comment_title in the MySQL database. You should be able to apply this to many other things and use it as a primer to get you started really messing with your wordpress files.
WARNING: Just remember to save backups to all the files you have been messing with.
Comments
Comment from Todd
Time: February 1, 2007, 12:09 am
I once neglected to set up backups with some work and it really does take a significant amount of backpedaling to figure things out when they go wrong!
That is a cool little hack, I might need to do something similar in the near future.
Comment from bird
Time: February 19, 2007, 7:46 pm
Why aren’t you using this hack on this page? Didn’t it work out in the end? I’d like to see the example before I try it…
Comment from bird
Time: February 20, 2007, 1:59 am
OKAY. This works. REALLY WELL. Thank you! For Wordpress 2.1, the code that WAS in functions-post.php and comment-functions.php is now in wp-includes/comment.php…. but otherwise, it works like a charm. You should make a plugin, no? Anyway, it’s great, thank you. I couldn’t find this information anywhere else!
Comment from Erik
Time: February 20, 2007, 2:06 pm
Thanks bird!
Yeah, I haven’t updated this post in a while, maybe I should write a quick little ditty about it?
But anyway, I use this mod on my page at http://www.sprintrants.com under the rants section. Check it out.
Also, feel free to leave your URL so we can see it in action ![]()
Comment from Allison Brown
Time: August 27, 2007, 6:06 am
Well done!
Comment from DiseƱo Web
Time: March 8, 2008, 7:37 am
Woa! Erik nice solution, if someone search: add extra fields in comments.php or extra fields in comments on WP is very difficult find something or few.
I find only this and another solution but the other doesnt works.
So Erik if you transform this in a plugin, you made a Big Wordpress contribution.
Im glad to see the 1st plugin to make this.
Congratulations for the Idea!
Comment from Hone
Time: January 18, 2007, 12:53 pm
Hi Eric just wondering why Comment Titles isn’t implemented in your blog here?