Ultimate Guide: Comment review system

Since there are many ways  to integrate into comment form and comments to add review system that allows submittal of ratings/reviews with the comments, I decided to put on new tutorial that shows different scenarios for this, and solutions for each one of them.

Comment Form

First part of integration is to add rating block into comment form.

Method A: Theme uses comment_form() function.

Most themes now use this method. It is easy to spot: in comments.php (usually) theme uses comment_form() function. With some themes it might not be there, but I can’t help you with that, you must locate it and be sure that that is the method used.

You need to add this code into the functions.php. First example here is to add standard single rating into the comment form.

[php]add_action("comment_form", "gdsr_comment_form_integration");
function gdsr_comment_form_integration() {
echo ‘<p>’;
wp_gdsr_comment_integrate_standard_rating();
echo ‘</p>’;
}[/php]

And, second example is to do it for the multi set, and the number 4 used in that example is the ID of the multi set, and you need to replace it with the ID of your multi set.

[php]add_action("comment_form", "gdsr_comment_form_integration");
function gdsr_comment_form_integration() {
echo ‘<p>’;
wp_gdsr_comment_integrate_multi_rating(4);
echo ‘</p>’;
}[/php]

Method B: Old type of comments form located in comments.php

This old method for comment form is used by most older themes (and some new use it still). It is easy to spot, since the comment.php contains actual form with all elements, and you need to look for TEXTAREA tag. Name attribute for this HTML element is ‘comment’. Following code should be added before this tag, so that rating block is displayed above it. Depending on the theme, you might use different HTML elements to envelop the rating block for better styling. I can’t help much with this, CSS is theme dependent and it has nothing to do with rating or this plugin.

First example is for standard rating:

[php]<?php if (defined("STARRATING_INSTALLED")) : ?>
<p><?php wp_gdsr_comment_integrate_standard_rating(); ?></p>
<?php endif; ?>[/php]

And, second example is to do it for the multi set, and the number 4 used in that example is the ID of the multi set, and you need to replace it with the ID of your multi set.

[php]<?php if (defined("STARRATING_INSTALLED")) : ?>
<p><?php wp_gdsr_comment_integrate_multi_rating(4); ?></p>
<?php endif; ?>[/php]

Ratings in the comments

Now we need to add rating values saved with comments into the comments displayed with the post.

Method A: Theme uses wp_list_comments() function, with default callback.

Most themes use this method, and this function relies on another callback function to display individual comments. WordPress has own default function for this, but some themes can have own callback function. Find wp_list_comments in comments.php, and if it doesn’t have callback argument, than this method is for you. In some cases there are two wp_list_comments in comments.php, and one is usually for trackbacks and pingbacks.

Add this somewhere in functions.php (it is best to add it on the top or bottom, to know where it is). Function call has more parameters than needed, but we need last one ($echo) set to false. This will add rating block at the end of the comment text.

First block is for standard ratings.

[php]if (defined("STARRATING_INSTALLED")) add_filter("comment_text", "gdsr_comment_text");
function gdsr_comment_text($comment_text) {
if (!is_admin()) {
global $comment;
$comment_text.= wp_gdsr_comment_integrate_standard_result($comment->comment_ID, "", 0, "", false);
}
return $comment_text;
}[/php]

For multi ratings use second block, and make sure you have proper multi set ID (4 in this case), and it must match one used for comment form integration.

[php]if (defined("STARRATING_INSTALLED")) add_filter("comment_text", "gdsr_comment_text");
function gdsr_comment_text($comment_text) {
if (!is_admin()) {
global $comment;
$comment_text.= wp_gdsr_comment_integrate_multi_result($comment->comment_ID, 4, 0, "oxygen", 20, "oxygen_gif", "oxygen", 20, "oxygen_gif", false);
}
return $comment_text;
}[/php]

Notice: Function used for multi results must have names and sizes for the sets in the function call. For full specification of each of these functions, please check the source file legacy.php in folder code/fn.

Method B: Theme uses wp_list_comments() function, with own callback.

If wp_list_comments specifies callback function, you need to find this function (usually in functions.php). For instance, for TwentyTen theme, this function is called ‘twentyten_comment’. If you don’t want to mess with it, you can simply use Method A, and all will work fine.

But, if you want to have rating block located on some specific place in the comment, you might wanna change the callback and add rating block where you want it. So, you need to find callback function, and you need to make sure it is OK, and has comment_text() function call. Usually, you want to add rating before or after this function, but you can experiment and add rating code in some other place in the comment block.

Rating code to add, for single rating is:

[php]<?php if (defined("STARRATING_INSTALLED")) : ?>;
<?php wp_gdsr_comment_integrate_standard_result($comment->comment_ID); ?>
<?php endif; ?>[/php]

And for multi rating (for set 4):

[php]<?php if (defined("STARRATING_INSTALLED")) : ?>
<?php wp_gdsr_comment_integrate_multi_result($comment->comment_ID, 4); ?>
<?php endif; ?>[/php]

Method C: Theme uses old style comments loop in comments.php

If the theme doesn’t use wp_list_comments() function, than it usually relies on having full comment loop in comments.php. Simplest way to add rating, is to again use Method A. But, if you want to have own location for rating block you need to find comment_text() function call, and add rating block around it somewhere inside the single comment element in the loop. To do that use the code from Method B, but, replace $comment->comment_ID with comment ID if the proposed code doesn’t work.

Aggregated ratings in the post

Now we need to add rating values saved with comments into the comments displayed with the post.

Method A: Modify single.php or loop-single.php template.

This is the best method to use. But, you must disable auto insertion of the rating code (for standard ratings), to avoid getting duplicated code. For most purposes, you need single.php to do this. GD Star Rating doesn’t fully support post types, so it is not advisable to use it for that.

Find single.php template. In this file locate the_content() function used to display post content. In some cases single.php is not displaying post, but uses other template, usually called loop-single.php (TwentyTen and TwentyEleven themes do this). So, use this file to add code.

First example is for single rating:

[php]<?php if (defined("STARRATING_INSTALLED")) : ?>
<?php wp_gdsr_render_article(0, true); ?>
<?php endif; ?>[/php]

Second example is for multi ratings, and again, 4 is for multi set and it must match one used in comments and comments form.

[php]<?php if (defined("STARRATING_INSTALLED")) : ?>
<?php wp_gdsr_render_multi(4, 0, true); ?>
<?php endif; ?>[/php]

Conclusion

This covers most cases for comment integration. If you need to know and use parameters for wp_gdsr_ functions used, check out the Documentation for this plugin. In some cases, none of these methods will work, if the theme is not standard, if it has no usable templates (some theme builders), or if it uses own files to display different things. If that is the case, you must contact theme authors to get information about theme structure or you can ask for help in the support forums here. Use the forum to ask anything else you might need to solve the issues with integration.

Comments

Leave a Reply