On a recent project I needed to implement a products section. The products section needed a number of functional pieces that very closely aligned with that of built in Posts in WordPress. The site was already slated to have a blog, so hijacking Posts themselves was out of the question, but lead to the perfect application of a Custom Post Type.
There were some unique fields I needed to support some of the functionality of the Products section in its entirety, but in particular I was aiming to repurpose comments to support reviews. Changing a few labels got just about everything taken care of, but the client was looking to have a location field in addition to a name, email, and the review itself.
But… why not use Pods?
The short answer here is that Pods 2.0 isn’t out yet. Pods 2.0 would have made the need to build this additional functionality from the ground up unnecessary, flat out. Pods 2.0 is still under development, and with WordPress 3.1 RC3 available, we’re in high gear on getting things up and running. For the time being though, client work continues.
Reviews aren’t comments though
My strategy in building WordPress powered sites is to use anything and everything available in core before branching out to plugins. That includes manually writing queries, implementing my own hooks by hand, and generally resisting the need to use plugins wherever possible. I take this route because WordPress is a system in and of itself. To me it’s very important to build upon that system by taking advantage of what it’s got to offer.
In this particular case, I wanted the products section to be as searchable as possible, resulting in my decision to use a Custom Post Type. Additionally, WordPress’ comment system is great, why reinvent (and rewrite) the wheel? You actually have decent granular control over the comment experience, specifically when it comes to fields. What leaves a bit to be desired though, is the ability to use your own Custom Fields with comments. It’s not extensively documented, but there is database storage and core functionality for comment metadata built into WordPress as of version 2.9.
Continuing, while I do my best to use WordPress core functionality at all costs, I don’t stretch it. If something isn’t there, shoehorning is not a solution I default to either, I’ll build it in if a great plugin isn’t out there. When I do build additional functionality though, I do my best to see whether or not it would be useful to other developers. If the functionality is too unique, it’ll go into functions.php
or a private plugin. There are times though, where the functionality is generic enough to be useful on a more global level. Many times I’ll build in the functionality, and it’s only when I can pull together the time I’ll refactor what needs to be taken care of and ideally push out a plugin for everyone to use. Custom comment fields happen to be such a case, in my opinion.
Doing it by hand
While this article is technically an overview of the inspiration behind the plugin, I’d like to walk through how it actually works in case you’d prefer to do it by hand. There isn’t a whole lot to it, so let’s begin.
The first part will be making sure the field ends up on the front end in the first place. To do that, we’re going to hook comment_form_default_fields
and add our desired field, in this case we’ll be using Location.
function iti_review_fields($fields) { global $post; if( is_singular( 'iti_products' ) ) // this is our CPT { $fields['url'] = ''; $fields['location'] = '<p class="comment-form-location">' . '<label for="location">' . __( 'Location' ) . '</label> ' . '<input id="location" name="location" type="text" value="' . esc_attr( $commenter['location'] ) . '" size="30" /></p>'; $fields = array( $fields['author'], $fields['location'], $fields['email'], $fields['url'] ); } return $fields; } add_filter( 'comment_form_default_fields', 'iti_review_fields' );
Essentially what we’re doing here is appending a location
key to the $fields
array WordPress uses. That’s going to get our new comment field to show up on the front end, but only if we’re viewing our Custom Post Type named iti_products:
Awesome, our additional field has been set up, but the next issue to deal with is actually saving the submitted data. WordPress doesn’t accept dynamically submitted comment form fields (which is a good thing) so we’ll need to expressly accept that submitted data. This is easily accomplished with another hook, this time to comment_post
:
function iti_add_author_location($comment_id) { add_comment_meta( $comment_id, 'location', $_POST['location'], true ); } add_action( 'comment_post', 'iti_add_author_location', 1 );
That snippet can also be added to functions.php
and enables our newly created location comment field to be saved to wp_commentmeta
.
The final step would be to modify the way your comments are listed on the front end via wp_list_comments()
or otherwise. If using wp_list_comments()
with a callback, you’ll need to edit your callback function and manually pull the data from wp_commentmeta
— luckily WordPress makes this as easy as pulling post meta:
echo get_comment_meta( $comment->comment_ID, 'location' );
This process can be repeated for each additional comment field you’d like to use, and you can customize it on a per-CPT basis by using the is_singular()
conditional. With this process being so simple but at the same time being tedious with the possibility of quickly bloating and becoming very non-DRY, it leaves great opportunity for a plugin.
Things to be aware of
While our fields appear fully implemented on the front end, we haven’t taken any steps to customize the display or editing of this data in such a way that’s provided with standard comments. There are a few steps to take here, but at this point they’re pretty much hacks and I’ll be researching how to better integrate the editing process.
Plugin Status
This plugin is no longer actively maintained by me and has been removed from the WordPress Plugin repository. You can replicate the functionality as outlined above and enhance it as you wish.
Comments
I’m waiting for a few weeks now for a respond that my new plugin is added to the plugin repository. Seems like they take a break as I heard the same from Toscho (another famous German WordPress developer).
Now that you mention it, I remember them saying they were taking a break from accepting submissions. Thanks for the reminder!
[…] This post was mentioned on Twitter by Jonathan Christopher and Nicolas Messer, Devin Price. Devin Price said: RT @jchristopher: Wrote a bit about custom comment fields in #WordPress http://bit.ly/hbqqKc Plugin is imminent, stay tuned! […]
Silly question, where do we put this code into?
I downloaded the plugin and cannot get it to work in any version of wordpress, the plugin installs fine and i can see the comments fields editor, but there is no actual way to use the fields, the info in the plugin is very limited.
Please can you guide me here as this would be an awesome plugin once it works.
Have a look at the installation instructions — you’ll need to edit some theme files to get the data to show up in your comments list.
Jonathan,
Thanks for this plugin!
I’m with Art Awan above (I don’t have sufficient understanding to implement this plugin without more guidance than you have given us so far).
Here’s what I’ve done:
– downloaded and activated the plugin
– added the field “Summary” using the editor
– added: echo get_comment_meta(_iti_ccf_page_summary); to comments.php in the theme folder (is this syntax correct?)
– added an input field named “summary” to the form in comments.php
– Nothing.
– renamed the input field “_iti_ccf_page_summary”.
– Nothing.
Would you be so kind as to give a complete example of how it’s implemented?
(you know, for those of us who can copy-paste, but not code)
Thanks again!
Hi,
I’ve noticed that the additional field is displayed only when the user is not logged on. Is it a correct behaviour from WordPress? Is there any way to display the field even when the user is registered to the site and logged on?
Hi Jesse, I’m planning to do a screencast that outlines implementation directly, but haven’t had a chance to do so. The first thing I notice, however, is that you’re not passing a string value to get_comment_meta — yours should be
get_comment_meta( '_iti_cff_page_summary' );
That may in fact fix your issue.
That depends on how your comments.php (or associated callbacks) have been written. Many themes by default remove every field aside from the Message/Comment field if you’re logged in as it saves time. When using custom comment fields, however, you’ll need to make sure that doesn’t happen and all of the fields are pulled in regardless of whether or not you’re logged in. Hope that helps!
Great Plugin. Thank you very much!
But there is one thing I miss. The custom fields aren’t displayed to logged-in users. Could you fix this?
Unfortunately, the custom fields doesn’t show up in the notification e-mails for new comments.
I’ll add it to the feature request list, sure.
I’ll add this to the feature request list as well.
Thanks very much for writing this plug-in.
It’s a shame that WP have such limited built-in functionality regarding the comments submission form (the default validation is a joke) – so I’m sure you’re saving everyone (including myself) a huge amount of time and annoyance.
A couple of things though;
* Is it possible to add the functionality to edit the extra fields through the WP’s Comments Admin section?
* In the documentation, it says:
‘Implement get_comment_meta(%FIELD_NAME%) replacing %FIELD_NAME% with the name provided in Settings > Custom Comment Fields’
– I’m presuming this is referring to where we would like to output the information collected by the extra fields? So I’ve put the following code in my wp_list_comments loop:
– But it doesn’t echo anything. Can you help?
Just to clarify with point #1 above, I’d like to be able to edit the information gathered by the extra comment fields in the WP back-end, at the moment it just lists the core fields info, i.e. name, email, url, comment.
UPDATE:
For anyone else trying to output the extra field info into their comments loop, I used the following code which worked for me:
comment_ID, ‘_iti_ccf_page_YOURFIELDNAME’, true ); ?>
Thanks again!
Another comment (sorry):
Would it be possible to add in functionality to include other types of form input elements, i.e. Drop-down menus, radio buttons, etc?
Just a thought.
That’s definitely in the roadmap but I haven’t had the time to work on implementation quite yet.
Sure, I can add that as a feature request. I’m currently focusing on Pods 2.0 which should in turn help provide some insight on implementing that functionality in this plugin as well.
Where do I have to put “get_comment_meta(%FIELD_NAME%)” in the default Twenty Ten theme?
I think you should extend the installation instructions. It took pretty much time to get it working.
For example, someone without programming knowledge might miss the “echo” or “(s)printf” statement before the “get_comment_meta()” function.
You could also add the information, that in the default theme “Twenty Ten” the modification has to be done in the functions.php file.
The code has to look like this:
echo get_comment_meta($comment->comment_ID, ‘_iti_ccf_post_your_field_name’, true);
Thanks for the info very well done
Got the plugin installed and my echo get comment meta function working, BUT – the form isn’t saving anything when I submit. What’s missing?
You can confirm the data is not being saved to the database? Please feel free to send me an admin log in and I’ll check it out.
never mind! It was saving the comment meta key but not the value, and I fixed it by adding my own custom comment_post hook in my functions.php file and now it works wonderfully. Thanks anyway for your quick reply, and of course for the plugin itself!
Great plugin – thanks.
Any way to make the custom fields required like name and email?
Not quite yet, but I’ll add that to the feature requests for sure!
[…] comments. It turns out the documentation is sort of sparse in regards to doing so, but a few helpful posts, and some digging inside of the comment-template.php core file finally got me […]
I did little tutorial on how to add custom fields to wordpress comments which also addresses the issue of the fields disappearing when the user is logged in. Check it out for the quick fix for that.
Any news on being able to edit comment meta data?
Nick I have NO php programming knowledge and have been trying to figure out where the “code” goes for over a week..I too and using the Twenty ten theme . I figured out it went in functions.php but having no programming knowledge. I was truly at a loss for where I was to put the code.. Thank YOU! I’m crossing my fingers I can get this to work
Nice. I just installed this on a client’s site I’m working on, and it’s working wonderfully. (WordPress 3.3.1, your plugin version 0.1.2). Just thought you’d like to know – working a treat. I’m using it to add Twitter, Facebook and Blogger profile page urls (and it’s pulling the avatars from them to use in the comments, so you don’t have to rely on the Gravatar system. Looks for Blogger first, then twitter, then gravatar).
The only thing I wish would be that the fields would be editable in the back-end of WordPress – because right now, I’m going through phpMyAdmin to do it to currently existing comments.
Oh, and two more things: 1) I know in my last comment I’d mentioned I wanted to b able to edit these fields in admin: I know you’re looking into that. it was just a general “still wanting this” kind of thing 🙂
2) As I’m in phpMyAdmin I notice a possible bug. I created 4 extra fields. When I posted a test comment (using theme extra fields) it put in the info for the 4 extra fields just fine… BUT… it created the 4 extra fields for all other previously-made comments on the site… 4 times. So for each comment that has no use of the extra fields (i.e. no info put in) the new fields were created a total of 16 new rows (4 fields x 4 times = 16 new rows). When I create a new comment with the extra fields, 20 rows are added: 1 with the information, and the other 4 repeats as is found for the comments with no information.
Just thought I’d mention that.
Glad you’re liking the plugin, Shelly! I haven’t had much time to work on that particular plugin lately but adding editable fields to the WP Admin is first on the list as soon as I can wrangle the time.
Thank you for the details, I’ll be sure to check into that as well!
Jonathan,
Thank you very much for your tutorial. This is exactly what I was looking for.
On another note, I very much agree with trying to use what WordPress has to offer to its fullest, if at all possible. Your solution, while custom, still piggybacks off of existing WP functionality.
– Don
I’m liking your plugin and works like a charm, with the exception that adding a extra fields do not show up when the user is logged in, even with the default functionality, any chance of pointing us in the right direction? Thank you
Is the plugin still available? It doesn’t show up in the WP directory.
Hi Perry, unfortunately I haven’t had the time to update the plugin in over a year so I’ve had it removed from the plugin repository. I apologize for leaving this post out of date but the code referenced still applies to WordPress and you should be able to add it in via a plugin of your own creation or even functions.php if you feel that’s a better way to integrate. There are also other plugins out there that accomplish the task I believe, but I haven’t used any personally.