
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:
- integrating the rating inside the comment/review box, and displaying its result
- validate the review upon submission
- 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:
- 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.
- 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->comments WHERE comment_post_ID = '$comment_post_ID' AND comment_approved != 'trash' AND user_id = '$user_ID' "; if ( $wpdb->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) : ?> <!--?php if ( ocl_new_comment($user_ID, $post--->ID) ) : // ROW #1 to ADD ?> |
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!