GD Star Rating – Integrating Comments

IMPORTANT: Post is quite outdated and no more follewed by the author. Use it at your own risk!

As you may already know GD Star Rating is an all-inclusive rating/review plugin for WordPress. It has got tons of features and options and I think it is even better that all other premium solutions.
The only problem about GSR is the lack of documentation, even searching the web the only places to learn something from are its little official tutorials and the costly premium support forum.

Therefore I decided, after struggling for hours with it, to share all the little things I discover about it in this blog posts. Actually I am trying to create a rating platform, so users instead of writing comments will be writing reviews and rating of the post.
Fundamental part of this are:

  1. integrating the rating inside the comment/review box, and displaying its result
  2. validate the review upon submission
  3. allow only one comment/review per user

Let’s get through them, this is how I did, of course an expert coder would do so much better..

Displaying the rating box

This part is pretty well explained in this two tutorials part 1 and part 2, it just consists in inserting two snippets of code, but the problem comes at the end, when you need to modify the Walker class for rendering the comments and showing the rating. Well I don’t even know what a Walker class is, but I managed to easy solve the problem changing theme. One I found to work smoothly is Modernity, this theme (at least the way it uses to save/display comments) makes it also very straightforward to insert the snippets of code cited above, while using the default twentyten, for instance, you I had to edit some core files to make it work (since it uses a completely different way to use comments)! Furthermore Modernity is a very simple and clean theme to start creating your own theme with.

Another way to achieve this is by getting Strascape theme (made by GSR creator as well), and using its comment files, but I have not tried it yet.

Important notes

These are really important stuff, at least they were to me since some it took me a day to figure them out:

  1. While using Multiple Ratings inside comments, if you try to comment and you leave some values empty, it will only display the comment and the ratings will be discarded. Thus we’d better add some alert or error page if the user forgets to rate them all. Otherwise you can enable the null valuation in the ‘integration’ settings tab.
  2. If you only need to use Multiple Ratings integrated inside the Comments (like me)  you can uncheck all the settings for auto-inserting ratings in articles and comments in the settings page; you can also set vote rule to close and hidden everywhere apart from both articles rating and multiple ratings.

Form validation

In my rating website I want people writing reviews/comments to also rate the post, therefore I am using some javascript to validate that all the fields in the multiple rating has been rated; I also use the code to check (before all wp controls) that the comment/rating is not empty. First of all let’s embed jQuery if it isn’t already:

<script type="text/javascript" src="PATH_TO/jquery-1.4.2.min.js"></script>

then I used the following code, and put it in comments.php theme file,:

<script type="text/javascript">
      $(document).ready(function() {
         $('#commentform').submit(function() {	 
		 var risp = false;
		$(".gdcurrent").each(function(){
			if ($(this).width()==0)
			{
			risp = true;
			} 
		})
		  if ($('#comment').val() =='') {
		  $("#error").text("Write a review!").show().fadeOut(2000);
			return false;
		  } else if (risp) {
		  $("#error").text("Rate all the fields!").show().fadeOut(2000);
			return false;
		  } else {
		  return true;
		  }
		});
      });
 
 
</script>

In this way as the user submits the form we check if it’s OK, otherwise an error text will be displayed.

And I added the paragraph showing the error message at the end of the form div:

<p id="error"></p>

Limit comments
To do this I created the function ocl_new_comment, that looks if into WP database there is another comment from the same user in the same post.

function ocl_new_comment ($user_ID, $comment_post_ID) {
 
	global $wpdb;
	$dupe = "SELECT comment_ID FROM $wpdb-&gt;comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved != 'trash' AND user_id = '$user_ID' ";
	if ( $wpdb-&gt;get_var($dupe) ) {
		return false;
	} else {
	return true;
	}
}

This one has to be added to your functions.php file. Then in the comment.php theme file we call the function (add just the rows marked as ROW#1 and #2):

<!--?php if ('open' == $post--->comment_status) : ?&gt;
<!--?php if ( ocl_new_comment($user_ID, $post--->ID) ) : // ROW #1 to ADD ?&gt;

Leave a reply

//OTHER COMMENT STUFF...
 
<!--?php else : // comments are closed ?-->
		<!-- If comments are closed. -->
 
You have already written a review.
 
<!--?php endif; // ROW #2 to ADD ?-->
<!--?php endif; // if you delete this the sky will fall on your head ?-->

And that’s it!

Comments

  1. S Davis says:

    I know what you mean by having a heck of a time trying to figure out how to work this thing. Maybe you can help me. What I’m trying to do is have ratings categories, say like five of them. they are my ratings ONLY. Not for users. So I need to set them and have them locked. I have created these categories in the Mulit-set area. But I have no clue as to how to get this whole thing to work. Any thoughts?

  2. teocomi says:

    Hi Davis,
    it is pretty easy, you have to:
    1. activate multiple ratings
    2. enable ratings embedding inside the admin page
    3. enable multiple result display inside the post (to do this maybe you have to manually insert some code)

  3. SEbastian says:

    hey

    you forgott a bracket “}” at the end of your function for the function.php

    cheers

  4. Simon Schaudt says:

    Hallo teocomi

    Have you an idea for the following output with GD Star Rating:

    In a new Blog, there are 6 Article.
    User can comments the Article and vote every comments.

    Now, i would like the following output:

    - Top 20 comments voting from per article
    - Top 20 comments voting from all
    to integrating in a site (not in a widget).

    Thanks a lot for check and answer, Simon

    PS:
    –> http://blog.teocomi.com/home/
    We are living …
    Its so – now, the world “do” a change, its time :o)

  5. Mads Polsen says:

    Hi

    I really like this plugin. I’m using it on my site and I’ve actually three different kinds of installations.

    trendlists.com -> Here I use the plugin to rate a “bunch” of sites together.

    edu.trendlists.com -> This one is new, but here I target category pages and have nofollowed the post pages to avoid duplicate content.

    coupons.trendlists.com -> Here I use GD star together with Couponpress and it workd prefectly :-)

    I’ve done quite a few installations of GD star and if someone needs a bit help, just send a mail or answer to this comment.

    Btw, my site is featured on the GD star website, there is also exaples of other cool implementations on that page, http://www.gdstarrating.com/showcase/

    Thanks
    Mads

  6. Paul says:

    Hi,

    I cant get any of this working!

    Dont understand the 2nd part just add ROW #1 #2 etc.

    And the first part wont work either, i can still submit without filling in the stars.

    Any help?

  7. Paul says:

    I have got the second part working to stop the same user posting more than once on the same post. However this only works after i have submitted the comment/review then refresh the page.

    If i dont refresh the page, the comment form is still visible and i am able to submit another comment.

    How can i get it to remove the comments form after the comment is submitted rather than having to refresh?? So as soon as the user submits their comment the form has not visible to them

    Hope you can help me out, cheers

  8. Chris says:

    Great post,

    I will be looking to implement these checks shortly after resolving this issue!

    ISSUE: I need to write (2) php statements to display different multi-rating sets based on the category the post is in.

    OVERVIEW

    I have successfully integrated multi ratings into my posts using the tutorial 1 and 2 at the gdstar site …..

    I have an interactive block included in the comment section for voting.

    =================================

    (Voting Block) included in comments.php

    Rating

    =================================

    And another (read-only) in template funtions to show results within the users comment.

    (Display Results Block) included in template_functions.php

    =================================

    BUT, I need to replace both of these with statements that will render a different multi-rating set based on the category the post is located in.

    If viewing post in category 6 show multi-rating set (2)
    If viewing post in category 7 show multi-rating set (3)
    If viewing post in category 8 show multi-rating set (4)
    If viewing post in category 9 show multi-rating set (5)

    else if viewing post in other category show multi-rating set (1)

    Thanks for any help.

    • Chris says:

      UPDATE:

      By simply duplicating the following in comments.php and changing the rating block id from 2 to 3 I’m now able to show a different multi rating/voting block based on category:

      Rating

      Rating

      Not sure if it’s okay to do this way WITHOUT using (if else) but it seems to be working.

      ====================
      ISSUE:
      ====================

      Trying to display different multi ‘results’ blocks based on category (template_functions.php)

      Copied the following like I did above for comments.php

      This way does display correct blocks based on category, BUT the comment is being shown twice.

      I’m guessing a simple cut and paste and change cat and block ID won’t work here, probably needs the if else.

      I’m getting close to making this work and would really appreciate any help in setting this up correctly.

      Thanks.

  9. David Z says:

    Hi there,

    I’ve been tinkering with GD Star Rating for the past 3 days. Sadly, I still can’t figure out how to get the multi-star rating set I made to show up under the Comments.

    Can you help? Provide some guidance here?

  10. Mark says:

    Hi thanks for the article. You are right about the lack of documentation. I have been fighting GD for the last 28 Hours it feels like.

    I have actually achieved the above and integrated comments successfully. This article is almost a year old, but there doesn’t seem to be any better options out there at the moment.

    Now my issue: Im trying to display user reviews (comments) with the rating in various list forms: ie Recent comments (reviews) lists etc etc.

    Now its pretty tricky to display comments, even though theres a hundred and one plugins that will achieve this. To get the query yourself (so you can attempt and fail to integrate GD into it) you need to find the SELECT DISTINCT comments query.

    But integrating the If GD statement i run into problems, it simply wont show up, im using this in functions and im not sure if that’s ok to do.

    If you or anyway has a way to display recent comments with the ratings please send me in the right direction.

  11. Rob says:

    I know this is a fairly old post now, but I’m having problems trying to aggregate my comment ratings (not the one for ratings on individual comments but the one for ratings added at the time a comment is written), I’ve seem to have read through a mass of documentation without getting anywhere.

    In some places it reads as though comment ratings will be added to the post / page rating, but there also appears to be a suggestion of a specific function for aggregating comment ratings.

    It would be useful to know which way round this is supposed to work if nothing else, so that I can at least give some direction to my research into a solution, but if anyone has achieved aggregation of ratings posted with comments I would appreciate any help.

    Thanks,

    Rob

  12. saeed says:

    Hello
    Your post is very helpful for me. I have used your provided code regarding comment validation. Can you please guide me that overall score (rating of product) doesn’t change until admin approve the comment.
    I will appreciate your help and support

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>