GD Star Rating: Comment integration with Wordpress 3.0

Published 2012-05-23 · Archived article

GD Star Ratings is a great plugin IF you look past the excess javascript and CSS (which you can easily disable). Why just “okay”? The documentation is out of date by at least 3 years at the time of this writing. It’s also a heavyweight plugin for sure – not something you want to use on a shared server.

The simplest tasks require you to dig through the plugins functions, and you really do have to spend some time familiarizing yourself with the plugin.

Inserting GD Star Ratings into comment_form

WordPress 3.0 replaced the comment form template with the function comment_form. To add multi-sets or standard ratings manually to the comment form is now an entirely different process. The trick is to use $echo=false when modifying comment_form.

Here is what I replaced comment_form() with to insert a multi-rating block in my theme (comment_form is likely called from the template “comments.php”):

<?php
$form_args = array( 
	'title_reply_to' => __('Reply to %s'), 
	'title_reply' => __('Post a Review'), 
	'comment_field' => wp_gdsr_comment_integrate_multi_rating($multi_set_id = 1, $template_id = 0, $value = 0, $stars_set = "oxygen", $stars_size = 24, $stars_set_ie6 = "oxygen", $echo = false).'<p class="comment-form-comment"><label for="comment">My Review</label><br /><textarea id="comment" name="comment" aria-required="true"></textarea></p>',
	'comment_notes_after' => __('')); 
comment_form( $form_args );
?>

This will add the multi rating block before the textarea and allow you to adjust your textarea label/class. The above code will use the stars set “oxygen” at 24px and multi set “1”.

Replace wp_gdsr_comment_integrate_multi_rating with: wp_gdsr_comment_integrate_standard_rating to display standard ratings


Display Rating Results in Comments

After you have added your rating block to comments, you will want to display the ratings alongside the comment. I am using my reviews in a custom post type, and I don’t display multi sets on posts, so what I did I create a copy of my comments.php and renamed it reviews.php. I then changed twentyten_comments() to twentyten_reviews.

Inside of your functions.php, create a copy your existing comments callback twentyten_comments and rename to twentyten_reviews. If your site only displays multisets, you can just modify twentyten_comments.

Somewhere within your comments callback, you will add:

<?php // Display multiset rating results
 wp_gdsr_comment_integrate_multi_result($comment_id = get_comment_ID(),$multi_set_id = 1, $template_id = 1,$stars_set = "oxygen", $stars_size = 24, $stars_set_ie6 = "oxygen"); ?>

or

<?php // Display standard rating results
 wp_gdsr_comment_integrate_standard_result($comment_id = get_comment_ID(),$multi_set_id = 1, $template_id = 1,$stars_set = "oxygen", $stars_size = 24, $stars_set_ie6 = "oxygen"); ?> for standard ratings

Note: I wrote this post after being awake for over 24 hours and sitting at the ER waiting for a friend. I will try to rewrite this ASAP, but I cannot guarantee that I will. YMMV! If you have any questions, just ask!

Discussion

Previously published comments, retained for reference. New comments are closed.

Andy

Hi there, I tried this out with wordpress v3.41 and the first piece of code worked great allowed multi star rating. When I tried to add the second block of code: <?php // Display multiset rating results wp_gdsr_comment_integrate_multi_result($comment_id = get_comment_ID(),$multi_set_id = 1, $template_id = 1,$stars_set = "oxygen", $stars_size = 24, $stars_set_ie6 = "oxygen"); ?> Nothing happened. Do I need to add it ni a specific location in the comment callback? Cheers Andy

Kris

Tried the above but can't seem to get it to work. @Chris, I sent you guys an email a few days ago for some further assistance - hope you received it and look forware to hear from you.

Chris Rutherford

Sending you an email now.

mike george

Hi Chris, thanks for this tutorial but the 1st bit of code shows syntax errors and doesn't work - any chance you know what the problem is? Thanks.

mike george

Actually 1st bit works now just the second bit doesn't and again where do I put this bit (the multi snippet) thanks again.

Ryan Dennler

Hello, I'm building a website for a client and I'm trying to accomplish this, it seems like everything works only it puts the rating bar above the entire comment form no matter what i do ... see screenshot here: http://tinyurl.com/commentratings Thanks for the informative article, learned a bit about how to customize aspects of the comment form without doing any heavy html/css/php work by using $arguments instead.

Ryan Dennler

I've also tried placing it last in the comment_field section just to see if order of comment fields within this argument would cause any change and it still places it above the entire comment form. Could it be something theme related?

Chris Rutherford

Ryan, I have a version of a site we were using this on (we went a different route in the end). I'm loading it on my dev server and will see if I can find an answer for you.

Chris Rutherford

Ryan, I don't have time today to dig through the dev site and see exactly what we did, but a quick look suggests that we simply put "Post a Review" before the comments template was called, and then within comments.php we removed "Post a Review". You can put "My Review" above the rating block with something like: [php]'comment_field' => '<p class="comment-form-comment"><h5>My Review</h5>'. wp_gdsr_comment_integrate_multi_rating($multi_set_id = 1, $template_id = 1, $value = 0, $stars_set = "pig", $stars_size = 24, $stars_set_ie6 = "pig", $echo = false).'<textarea id="comment" name="comment" aria-required="true"></textarea></p>',[/php]

matthew moore

Curious can you do a write up on what exactly was done as I needed this function and unable to get it to work.

Elliott

Hi Chris, First off, thanks for post. It really saved me a lot of time trying to figure out how the plugin works. The problem I'm having is the same as Andy. After I placed the code in functions.php to show the results it did nothing. I'm using the TwentyEleven theme and not sure what I may be doing wrong. Also, if it's not too much to ask. Do you know how I can place the average results between all the comment ratings at the top of the post? I'm rating neighborhoods and using multiple categories such as location, safety, road access, etc and ideally the user would be able to rate while leaving their review but I am trying to get the total average rating at the top. Thanks in advance, any help is appreciated.

Elliott

- UPDATE - For anyone having the same trouble I was having, it turns out I was pasting the code in the wrong place on functions.php. You should paste it under the following get_comment_time() ) ) ); ?> #RESULT CODE HERE It would still be cool if anyone knows how to get a total average between all results on a post.

Marlena

I'm trying to do the same thing as Ryan. I can't understand the interfaces of GD Star for the life of me. Im also interested in doing a multistar rating that allows the user to attach some comments to their star ratings in order to explain their answer. Is that possible? Thanks

Ryan

Hello Chris,. Thank you this helped me output the rating block within the comment insertion form. My next issue is that I can't seem to get the results for each comment's rating to show up in the comment block. Do you know what settings and code you used to display the rating of the comment? Thanks

Ryan

The code I had used in the example doesn't seem to be working for me so I tried both multi and single variations to check and it just doesn't output. I'm plugging this into a themify wordpress site so it uses a custom comment callback function that I'm putting this in. I can get it to show a rating option FOR the comment, but I'd like it so that when a person makes a comment, he can also select the rating and that rating they selected outputs within the comment list with the comment they provided. I'm using this basically as a 'review' section where people can rate and write a review on the post. Thanks!

Ed

Try uncheck "prevent saving more than one rating per user" under integration menu. That does the trick for me.

Marz

Hi, I am trying to get this to work but I am getting a warning after step one: Warning: Invalid argument supplied for foreach() in C:\XAMPP\htdocs\dev\wp-content\plugins\gd-star-rating\code\t2\render.php on line 445 same old form shows but no star ratings. I am using GD Star Rating 1.9.22 and WP 3.5.1 Thanks

Chris Rutherford

Just found some comments that got marked as spam. If any of you still need help, email hello @ placementedge.com

animesh

I am trying to get this to work but I am getting a warning after step one: Warning: Invalid argument supplied for foreach() in C:\XAMPP\htdocs\lcoalwp\wp-content\plugins\gd-star-rating\code\t2\render.php on line 445 Some one suggest me what we do for that

Mehrwash Faruqi

Hello Do you know how I integrate Multi Review block in the Comments section? Thanks

Chris Brown

Okay, so first things first. This is an excellent tutorial - especially when you consider how poor the official support is! Unfortunately my lack of PHP is getting the better of me. Would anyone be able to help me troubleshoot the following problems: 1. When I insert the multi-set rating within the comment form the multi-set criteria titles appear in a table but the star's don't appear. I'm wondering whether this is a CSS problem? 2. I have no idea how to copy the comment callback function to get it to work. I use the Canvas theme by Woothemes and their comment.php is very different to Twentyeleven! I have attempted to duplicate the callback, change the function name and then paste the second part of the code in but I then get a nasty warning across my website re 'The header'. I know this is a bit longwinded. The resources for integrating multi-ratings with comments are really quite scarce so any assistance would be really appreciated! Many thanks!

Steve D

give up and use something else. you will get really far then hit a brick wall. It had so much potential but then just got forgot about.

Chris Rutherford

I would recommend not using GD Star Ratings. While I have it working on several of my projects, I'm not a fan. I will try rewriting this tutorial using the latest WordPress, but we are absolutely slammed at the moment.

Steve D

Chris R. Do you have any good alternatives? Thanks

Chris Rutherford

It depends upon what you are trying to accomplish. If it is a review type site, you might be better served using Drupal. There are situations where GD Star Rating will get you what you need the fastest, but it requires a long learning curve. If you want to email [email protected] and let me know what your needs are, I can provide a recommendation for you. By the way, I looked over a site using this code, the code I'm using is now a bit different. I'm calling a custom review template on the page where reviews appear: [php]comments_template( '/reviews.php' );[/php] Then, within reviews.php we have: [php] $form_args = array( 'title_reply_to' => __('Reply to %s'), 'title_reply' => __('Post a Review'), 'comment_field' => '<p class="comment-form-comment"><h5>My Review</h5>'. wp_gdsr_comment_integrate_multi_rating($multi_set_id = 1, $template_id = 1, $value = 0, $stars_set = "pig", $stars_size = 24, $stars_set_ie6 = "pig", $echo = false).'<textarea id="comment" name="comment" aria-required="true"></textarea></p>', 'comment_notes_after' => __('')); comment_form( $form_args ); [/php] as opposed to the default: [php]$form_args = array( 'title_reply_to' => __('Reply to %s'), 'title_reply' => __('Post a Comment'), 'comment_notes_after' => __('')); comment_form( $form_args );[/php]

matthew moore

I can not wait for an updated tutorial.

Chris Brown

Oh, that's a real shame. Having exhausted the Wordpress plugin repository for a decent star rating plugin none of the current offering suit my needs. I would have thought that some budding developer would have introduced a TripAdvisor style, multi-review and comment plugin. Apparently not! Even the very expensive MyReviewPlugin is no longer supported as the developer has gone MIA.

Chris Rutherford

Unfortunately it's not a feature that is in high demand. Took a look at your website. It looks like a great candidate for BuddyPress/Drupal.

Mike

We got everything working using an old walker class found here: http://v1.gdstarrating.com/2009/05/28/comments-integration-2/ Just had to comment out two of the returns. See it in action at http://www.norcodrene.com/reviews

ketan

Thanks a lot....................!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

VideoPortal

This is possibly the simplest one of these, it lets you display the star rating wherever you want via shortcodes. It also has a responsive WordPress admin panel and lets you decide if you want to limit one rating per visitor or if you want it to be unlimited.