How-to fix the hReview snippet for GD Star Ratings

Recently it becomes much easier to get your rating into the search results of Google. Using hReview rich snippet code in your blog post can raise the CTR in Google if the rating is visible. The GD Star Ratings plugin has already a function to show the snippet beside the stars widget, but the markup is according the Google Webmaster Help section not valid. We wrote a small function that will fix this problem.

First disable the automatic ratings feature

and (if already enabled) disable the rich snippet feature, too.

Add this function to your themes function.php file:

function getGDratings($postId, $postName, $maxRating = 10) {
	$ratings = wp_gdsr_rating_article($postId);
	$rating = round($ratings->rating);
	$rating_count = $ratings->votes;
	if ($rating_count > 0) { // show the snippet only if there are votes
		return '
<div class="hreview-aggregate">
	<span class="item">
		<span class="fn">'.$postName.'</span>
	<span class="rating">
		<span class="average">'.$rating.'</span> out of
		<span class="best">'.$maxRating.'</span>
	</span> based on
	<span class="votes">'.$rating_count.'</span> ratings.
	} else {
		return '';

Next open the single.php theme file and add this code below the the_content() function surrounded by PHP tags (if needed).

echo getGDratings(get_the_ID(), get_the_title());
wp_gdsr_render_article(10, false);

These two functions are required to show #1 hReview snippet and #2 the ratings widget. The code should work in the most comment WordPress theme files.

25 thoughts on “How-to fix the hReview snippet for GD Star Ratings”

  1. says:

    Thanks for the code. I was trying to change the text position Rich Snippets but was not working. With your code I could display it where I wanted.

    Congratulations for the work.

  2. thanks for this code, but do you know how to add image from the post with de star rating ?

    thanks jen

  3. thanks for you reply, sorry for my english, for example if I put this code on my posts


    i have the image on the google rich snippet test but not the start, if i activate gd star rating rich snippet, i have the star but no the image on the google rich snippet test :-(

    i would like to have image and star…

    1. i would like to have image and star…

      Sorry but this is not possible (like I mentioned in my comment before)
      You need to decide what’s more important for you or the content/results

  4. Hi Jenny,
    I think you’re talking about another image.
    It’s possible to have the authors image beside each google listing (search google for “php download file”). This works with the rel=”author” attribute, but there is one bad thing: If google shows your profile image, the star ratings are gone :(

  5. ok thanks for your help

    1. You’re welcome Jenny!

  6. This is brilliant….I noticed the same and your solution was spot on!

    Moreover, I wanted to change the text in the Snippet. Much easier now.

    Thanks again, great work!

  7. Hey! function is almost perfect..! As the GD star rating shows you the ratings with one decimal. As an example 9.4 your functions uses round, which disbales that and only lets you show integers. So 9.1 will be 10 with your function which is nice.. But google wont like it, so if you want to play nice.. AND be able to have your rating with one decimal

    Change this

    $ratings = wp_gdsr_rating_article($postId);
    $rating = round($ratings->rating);
    $rating_count = $ratings->votes;


    $ratings = wp_gdsr_rating_article($postId);
    $rating = ($ratings->rating);
    $rating_count = $ratings->votes;

    just delete the round.

    THanks for an awesome function!

  8. Hi Carl,
    I think the “round” function isn’t wrong here, the function will round the value up and down (9.4 => 9 and 9.8 => 10). The most real value would be:

    $rating = round($ratings->rating, 1);

    The second attribute in that function will output “one” decimal.

  9. Does the second part of this tutorial need to be wrapped in some sort of php? When I add it to my themes single.php directly under:

    It does not work. The text:

    echo getGDratings(get_the_ID(), get_the_title());
    wp_gdsr_render_article(10, false);

    just gets rendered at the bottom of the page. Any ideas?

  10. Sorry looks like some of my code got lost.. should say directly under “php the_content”

    1. Hi Mike,
      you’re right this wasn’t clear in my post, I changed it a little bit. Thanks for the comment.

  11. Thanks. Shortly after posting this I did figure out that I needed the PHP tags and I was able to make it work after that. I was also able to work out how to display rich snippets for standard, multi and thumbs ratings, since my site has all 3. Thanks again.

  12. Hi guys, I have the problem that the stars are not appearing on google searches. If you google Desire Disco, mine’s at the top but the ratings have gone, why?

  13. Hello Matt,
    you can publish a valid rich snippet and anything else is up to Google. Do you checked your rich snippet using this tool?

    You have to know that Google shows the stars or any other data beside a search result only for websites they trust of data from authors with some authority. I checked your site and noticed that your articles are very new, you need to wait some more time to get your star ratings included. In the meantime you can work on your site, build more killer content which other people like to talk about. My Web Development Blog is many years old and it took several 100 hours of work to get all the rich snippets included.

  14. Thank you very much olaf. I will check through my site tonight. One interesting fact though is that my site used to have the stars, until the site was restored from a backup due to a hack incident.

    1. Check your site / page URL’s first using the rich snippet tool ;)

  15. Ok, I can see the problem, but don’t know how to fix it.

    1. I did a test on your homepage and only errors:

      Warning: In order to generate a preview, at least the following fields are needed: rating and either review count or votes.

      You need to follow the steps in my post (above).

  16. I did the test myself, I know what it says, I just don’t know what to do about it. The ratings appear on my page, just not on google searches :(

    1. That the ratings appear on your site will not say that your rich snippet is valid and in your code. Check my information in this article, you didn’t followed these steps right?

  17. I’ve done as you asked but now the vote stars have disappeared entirely off my page.

    I believe the code above only allows people to vote on posts?

    I want people to be able to vote on pages as that’s what people will see on google when searching for a mobile DJ in south wales :)

    1. Hi Matt,
      My article is about how to fix the rich snippet which show the stars in the Google results.
      It looks like you have trouble with using the GD Star Ratings plugin and I understand that. All these options are the reason why I never used the plugin for years. You can solve 99% or your star ratings using the plugin and the last 1% will be fixed with my function above (which is not written for the humble beginner). Actually this fix is only for those user who take care about validations, without this function your stars in Google are right too. I understand from your last comment that this is maybe not what you’re looking for. If you’ve trouble using the plugin, it’s possible that the plugin author is able to help on the WordPress forum? I don’t want to send you away, it’s more you’re asking the wrong guy ;)

  18. I understand, I’ll do what I can to fix my problem. Thanks for turing to help,

Comments are closed.