Customize WordPress Comment Request

I am trying to extend the functionality of comments in a WordPress installation. I read a hint of the elusive functionality of the "custom comment type", but could not find any information. Instead, I thought I would add a special column to the comments database table. This is the easy part. What I don't know how to do is set up comment requests to save, update, and read comments to reflect the existence of a new column in the table. I thought there would be a filter to change the query, but I can’t find anything to do this ... Any ideas?

+4
source share
2 answers

In fact, there is no user-defined type of comment, but you can easily and efficiently add columns using a “meta comment”, which is a table of name / value pairs associated with each name / value pair associated with this comment using 'meta_key' (Please do not add the column to the SQL database that has been denounced by the WordPress developer community.)

Suppose you want a user to add their Twitter account. This is the code that will keep my Twitter account in the comment marked with $comment_ID (adding a meta key name prefix to the underline is a good idea for any meta tag that you support with custom code compared to what you allow users choose a meta key):

 update_comment_meta($comment_ID,'_twitter','mikeschinkel'); 

Then, to load the value to display in your template, you simply call get_comment_meta() (the third parameter means that you need to return a single value, not an array of values):

 $twitter = get_comment_meta($comment_ID,'_twitter',true); 

Of course, not knowing how to connect WordPress to integrate this, the above functions will not help much. You need to use two hooks, the first of which is wp_insert_comment , which will be called when WordPress stores the comment:

 add_action('wp_insert_comment','yoursite_wp_insert_comment',10,2); function yoursite_wp_insert_comment($comment_ID,$commmentdata) { $twitter = isset($_GET['twitter']) ? $_GET['twitter'] : false; update_comment_meta($comment_ID,'_twitter',$twitter); } 

The second is a bit more complicated; one that allows you to add fields and change other aspects of the comment form. The hook 'comment_form_defaults' sets the default values ​​for the comment and allows you to add HTML for the Twitter field (I hooked the HTML format from the comment_form() function found in /wp-includes/comment-template.php on line 1511 in WP v3.0.1)

 add_filter('comment_form_defaults','yoursite_comment_form_defaults'); function yoursite_comment_form_defaults($defaults) { $email = $defaults['fields']['email']; $label = __( 'Twitter' ); $value = isset($_GET['twitter']) ? $_GET['twitter'] : false; $defaults['fields']['twitter'] =<<<HTML <p class="comment-form-twitter"> <label for="twitter">{$label}</label> <input id="twitter" name="twitter" type="text" value="{$value}" size="30" /> </p> HTML; return $defaults; } 

And here is what it looks like in action:

WordPress Comments form with a Twitter field

This extensibility of the comment form is new to WordPress 3.0, so by its nature it is new in an open source project, and probably it won’t take into account all the use cases (for example, there wasn’t an easy way to get a memorized value for the Twitter screen) . name), but I hope you can bend it as much as you want and get what you need, and in the future, released from WordPress, the comment form API will almost certainly improve.

Hope this helps.

-Mike

Postscript In the future, consider posting your question on a sister site of Qaru . WordPress Responses where most WordPress enthusiasts hang out, those who can quickly answer such questions.

+9
source

I found this useful link related to the topic:

Customizing WordPress Comments - Functionality and Appearance

0
source

All Articles