Rate v0.2 is pretty good, doesn’t break

I have received a lot of feedback from the WordPress community about my ratings plugin, Rate. I squashed some bugs and added some features and can now take this thing out of what I like to call “Beta.” If this thing breaks when you install, I am now officially an asshole.

You still need to insert the_rating() and the_comment_rating() into your theme – see screenshots on the plugin page – but I wrote some filters using the Plugin API that will insert the ratings widget into the comment form, so you can rate a post/page/product/thing while commenting, instead of after commenting. I probably should have started with this functionality, but it took a while to figure out the best way to do it.

Even though comment_karma is a field in the $wpdb->comments table, the comment_karma field will not be saved with the other comment fields if that field is Post’d along with the other values, so I actually have run a second database query after the comment has been inserted to save the rating (comment_karma) along with the comment data.

Weird and annoying.

Anyways, this thing should work like a champ now. I also fixed the_rating() to display a more accurate average of the ratings. Instead of just doing SELECT AVG(comment_karma), I added this logic: WHERE comment_karma > 0. Once again, duh, but at least it’s fixed NOW.

7 thoughts on “Rate v0.2 is pretty good, doesn’t break

  1. Hey,

    Been following this plugin over the weekend for a project we’re working on. The 0.2 update came at the perfect time!

    We needed to display the rating on the homepage, for each custom post type listed, so made the following modifications to your code:

    function rate_calculate($id = 0, $forceid = 0) {
    global $wpdb; //unmodified
    $url = get_permalink(); //unmodified
    $coerced_id = (int) $id > 0 ? $id : get_the_id(); //unmodified
    $coerced_id = (int) $forceid > 0 ? $forceid : $coerced_id; //new line

    That way, we can pass an ID through to rate_calculate, and force the DB query to use that ID. Definitely neater ways of doing this from your end mind …

    • rate_calculate() will take an ID as an argument, so you shouldn’t have to do that….

      This line is there to grab the ID from the loop, if no ID is passed to the function:
      $coerced_id = (int) $id > 0 ? $id : get_the_id();

      I should probably adjust the_rating() to optionally take an ID to pass to rate_calculate()

  2. Just looked back through the code … you’re quite right of course 😉

    For some reason, that wasn’t giving me the expected result. I’ll take a look again later.

    On a side note – I’ve noticied if submitting multiple comments, sometimes the rating isn’t sent as part of the AJAX POST request, resulting in a “0” entry for Karma.

    Very nice work though man!

  3. In case you get someone wanting to install Rate on the Thesis theme (and I’m willing to bet you inevitably will) here is what they’ll need to add to their custom_functions.php file so it works with the way Thesis handles comments:

    add_action(‘thesis_hook_comment_form_top’, ‘rate_form_filter’);

    function filter_comment_text($output) {
    $output .= the_comment_rating();
    return apply_filters(‘comment_text’,$output);

    I just had to do this on a client site I’m working on so I figured I’d share and hopefully save others the frustration!

    • I will add this into trunk when I get a chance – thanks for the heads up, I shiver when I think about the number of different setups out there….

  4. Hi Scott. Thanks for this awesome plugin. I am curious, is it possible to order posts in the loop based on their total karma?

  5. Hi Scott –

    Excited about this plugin – I’ve been looking for something very simple and clean like this.

    Two quick questions:

    (1) is it possible to have the rate system apply only to certain pages? I have a normal comment area on some parts of my site, but an estore on part of it as well and I’m trying to use the rate for the products in that estore. Would it be possible, for example, to apply the plugin only to children of certain pages (i.e., my store “home” page).

    (2) Is it possible to have the aggregated ratings for a particular page displayed somewhere else on the site via shortcode? So I have a home gallery with all sorts of products and I’d love to be able to take the total ratings from comments on those individual product pages and display just the aggregated stars with the thumb of each product on my product gallery page. Does that make sense at all?



Comments are closed.