开发者

Customize WordPress comment post query

开发者 https://www.devze.com 2023-01-23 05:11 出处:网络
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

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 'com开发者_运维问答ments' 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?


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):

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

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):

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

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:

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 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 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's what it looks like in action:

Customize WordPress comment post query

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

0

精彩评论

暂无评论...
验证码 换一张
取 消