I am trying to extend the functionality of the comments in a WordPress install. I read allusion to an elusive ‘custom comment type’ functionality, but could not find any information. Instead, I was thinking that I would add a custom column ‘ to the ‘comments’ database table. That’s the easy part. What I have no clue how to do is to customize the comments queries for saving, updating and reading the comments to reflect the existence of the new table column. I thought there would be a filter to alter the query, but I cannot find any that would do it… Any ideas?
Leave a Reply
You must be logged in to post a comment.
There isn’t really a custom comment type but you can easily and effectively add columns using “comment meta” which is a table of name/value pairs associated where each name/value pair is associated with a given comment using a
'meta_key'
(please don’t add a column to the SQL database; that’s frowned upon in the WordPress developer community.)Let’s assume you wanted to let the user add their Twitter account. This is the code that would save my Twitter account to the comment identified by
$comment_ID
(prefixing the meta key name with an underscore is a good idea for any meta that you maintain via custom code vs that you let users select the meta key):Then to load the value to display in your template you just call
get_comment_meta()
(the third parameter means to return a single value, not an array of values):Of course without knowing how to hook WordPress to integrate this the above functions would not be a lot of help. There are two hooks you’ll need to use, the first being
wp_insert_comment
which will get called when WordPress saves a comment:The second one is a bit more complicated; the one that lets you add fields and modify other aspects of the comment form. The
'comment_form_defaults'
hook sets the defaults for the comment and let’s you add the HTML for a Twitter field (I snagged the format for the HTML from thecomment_form()
function found in/wp-includes/comment-template.php
on line 1511 in WP v3.0.1)And here’s what it looks like in action:
This comment form extensibility is new for WordPress 3.0 so by its nature of being new in an open-source project it’s probably not going to accommodate all use-cases yet (such as there was no easy way to get a remembered value for the Twitter screen name) but hopefully you’ll be able to bend it enough to you will and get what you need and in future released of WordPress the comment form API will almost certainly improve.
Hope this helps.
-Mike
P.S. In future consider posting your question on StackOverflow’s sister site WordPress Answers; that’s where most of the WordPress enthusiasts hang out, those who can quickly answer questions like this.
I found this useful link related to the topic:
Customizing Comments in WordPress – Functionality and Appearance