Using comment form to set up review system

One of the most important uses of GD Star Rating plugin is to allow adding ratings for a post through comments form for a user/visitor reviews of the post. But, due to restrictions posed by WP and most themes, this is not auto plugin feature, it requires manual integration.

Here is how this works. You can’t mix comments and post ratings! But this is not that. Here you use comments form to add post ratings. Even the rating is submitted through comment it’s still post rating. You can still have comments ratings as independent rating system, and you can also display post rating saved through comment with individual comments. And we will see how to do all that. All this will be done based on WordPress Default theme (Kubrick based, version 1.6 from WP 2.9.2), but the principle is the same with any other theme. I have yet to find the theme that can’t be used for this. Method works with both standard rating and multi rating.

All my xScape themes support this by default, and all this is built into the framework. No custom work is needed if you decide to purchase any (or all) xScape themes, and all this will work out of the box, just needs to be activated using a option on the theme settings panel.

Comment Form Integration

Modifications for the comment form go to comments.php file. In case of TwentyTen theme, it’s not that simple since comment form is in separate function, so you need to modify that function, and that can’t be done directly. For themes using comment_form() functions, you need to use this tutorial.

For other themes, using old comment form system, you need to add example code from below somewhere before the TEXTAREA element (it depends on the theme where it is). First block is for the standard rating, second block is for multi rating. Multi rating requires multi set it to be specified (in this case number 4). For all other parameters these function support, check out the GD Star Rating source for this functions (legacy.php file in code/fn folder).

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

 

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

That’s it. This will add rating blocks into the comment form. You can add additional conditional checks to it and display this only for some category, or post type. You can also make rating required element so that user can’t submit form without rating. To do that you need to attach check to form submit. For single rating, you need to call functions: is_cmm_rated_standard() or is_cmm_rated_multis(). They will return true or false if the ratings are set or not.

Once again, all this, even validation is built in xScape themes and it can be controlled by simple settings in the theme control panels.

Display saved rating inside the comment

Once the rating is saved through the comment form, it’s added to the post rating summary, but you can also pull saved value and display it inside the comment that is used to submit it. This is a biggest issue with some themes, since there are several ways WP can display comments. Older themes (but still some new do that even if that is completely wrong) had comment display inside comments.php file. But most themes use wp_list_comments() function and additional callback function that is displaying single comment. Default WP theme uses default callback, so it’s not an option to change it. If you have own callback simply add this into it anywhere within the comment display. Comment ID parameter is required, and there is no universal method to get it, it depends on the theme and callback function, so you need to find out that on your own, I have left it as $comment_id in the function call. Again, first block for standard, second for multi rating. For other parameters supported by functions check the plugin source as in example above.

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

 

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

If you can’t add this to your own callback function, and you need to use default one, there is only one way to do it: use filter that is run inside the comment loop, easiest one is comment_text. 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. For multi ratings use second block.

[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]

 

[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.

Display overall rating in post

This works by default if you have auto insert option enabled. But, this will allow normal ratings, rating block will be enabled. If you want to only save ratings through comment form, you must display block in the post inactive so that only serves to display overall rating. So, anywhere in single.php template (after content maybe, function call the_content()), add function call like in the examples bellow, first one for single, other for multi ratings.

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

 

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

True as a value tells the function to render block as inactive, and it will only display overall/average rating.

Limitations

Ratings saved through comment form obey the same rules as ratings saved directly through rating in the post, so if options are set as by default, one one vote per user/visitor. If someone attempts to post more than one comment with ratings, only first rating will be used. IP and cookies limitations also apply, as well as post author rating option.

Use additional parameters for all the functions used here to change stars, or their size, template, and as I said, check out source code for all available parameters. If you don’t specify them, plugin will use default values from settings panel.

11 Comments

  1. Phyve says:

    Hi Milan,

    Great post! This one of the features I was looking for and exactly why I signed up.

    Seems to be one bug on my end, not sure why though:

    The ratings goes up successfully, the box shows up in each of the comments, but the stars are not showing up in each submitted comment.

  2. MillaN says:

    Multi comment result function was not set properly. Names for sets and sizes must be specified, since plugin is not checking right now if the values are valid. Check the updated article and new included notice in the ‘Display saved rating inside the comment’ block.

  3. Phyve says:

    Works Beautifully, Thanks for the update!

  4. Phyve says:

    Oops, just found a bug.

    It works great if you’re logged in.

    But if comment approvals are on, the comment gets posted without the multi-rating. It seems to get ignored completely.

  5. MillaN says:

    Ratings are saved regardless of the approval for comments. If the rating is not saved, something else is in play: duplicated vote or some restriction.

  6. Phyve says:

    You are correct, sorry. I was running a test on the same comment and because of my IP it was not allowing a second rating to be registered.

  7. Blue Horizons Ltd says:

    Line 80 in TwentyTen comments.php places the rating requirement to be ‘outside’ the comment form!? Are you sure that right?

  8. MillaN says:

    Depends what changed in that theme since the post is written. At least one update for that theme was released in the past 6 months, and it could be that the line numbers are no longer the same.

  9. Blue Horizons Ltd says:

    How would one expand on this and place the average rating in a post archive for example?

    For example, I have multiple posts with average multiset ratings enabled (blank default so I know multi set rating is working) and want to display multi set rating in place of the default core 5 star rating for each post snippet. I tried the following to override the core render_avg_rating() but I am still geting wordpress’s default 5 star rating:


    if (defined("STARRATING_INSTALLED")) add_filter("render_avg_rating", "gdsr_avg_rating");
    function gdsr_avg_rating() {
    global $comment;
    $avg_rating.= '';
    $avg_rating.= wp_gdsr_comment_integrate_multi_result($comment->comment_ID, 1, 0, "oxygen", 20, "oxygen_gif", "oxygen", 20, "oxygen_gif", false);
    return $avg_rating;
    }

  10. MillaN says:

    And where did you found ‘render_avg_rating’ filter in the first place? There is nothing like that in the plugin. Also, I don’t quite understand, you are mentioning the posts snippets, and here in the code using function for integration with comments. I think that best is to open new topic in the forum and explain what you need displayed and where. There are functions for all kinds of things, but I am confused here to what to use.

  11. Blue Horizons Ltd says:

    I got it. The third party plugin I use (Directory) has it’s own core files which I was mistaking for wordpress core files. It places a 5-star rating wihin each post excerpt. Replacing render_avg_ratings_of_listings() references for Directory core with wp_gdsr_render_multi() did the trick.

Leave a Reply