Filter: Comment-Free Ratings

Note: Using comment-free ratings requires Tasty Recipes 0.8.0+. Additionally, switching between comment-free ratings and comment-associated ratings is not supported. What you use is what you will stick with, so give this some consideration.

The default Tasty Recipes rating system only works when the default WordPress comment form is used. However, if you use a different comment system, such as a theme-specific one or a plugin like Discus, you will need to use a separate rating system.

Tasty Recipes supports an integration with the WP-PostRatings plugin. With this integration, clickable rating stars will be added to the recipe card in place of the default rating stars. Users are no longer able to leave a rating with a comment; instead, they are only able to leave a rating by clicking on the stars in the recipe card.

To use this integration, install WP-PostRatings on your WordPress website, then enter the following snippet into your theme's functions.php file:

***Before doing this, make sure that you have access to your site's files via FTP (the editor in WordPress is not sufficient in case problems arise) and that you are comfortable with editing your functions.php file. Any minor error in the functions.php file can result in the “white screen of death,” which is easily fixed via FTP but is difficult to fix otherwise (and a nuisance as your site will be unavailable until fixed).***

// Disable standard ratings UI
add_filter( 'tasty_recipes_enable_ratings', '__return_false' );

/**
 * Filter template variables to render WP-PostRatings UI
 * within the recipe card.
 *
 * @param array $template_vars Variables to be passed to the template.
 * @param object $recipe       Recipe object.
 * @return array
 */
add_filter( 'tasty_recipes_recipe_template_vars', function( $template_vars, $recipe ){
	if ( function_exists( 'the_ratings' ) ) {
		// Render the WP-PostRatings UI within the recipe card
		$template_vars['recipe_rating_icons'] = the_ratings( 'span', $recipe->get_id(), false );
		// Hide from display, but keep for microdata
		$template_vars['recipe_rating_label'] = str_replace( '<span ', '<span style="display:none;" ', $template_vars['recipe_rating_label'] );
	}
	return $template_vars;
}, 10, 2 );

/**
 * When a rating is saved for a recipe, put the data in
 * the place Tasty Recipe expects.
 *
 * @param integer $user_id User saving the rating.
 * @param integer $post_id Post ID for the rating (should be a recipe post).
 */
add_action( 'rate_post', function( $user_id, $post_id ){
	if ( 'tasty_recipe' !== get_post_type( $post_id ) ) {
		return;
	}
	$average_rating = get_post_meta( $post_id, 'ratings_average', true );
	$total_reviews = get_post_meta( $post_id, 'ratings_users', true );
	update_post_meta( $post_id, 'average_rating', $average_rating );
	update_post_meta( $post_id, 'total_reviews', $total_reviews );
}, 10, 2 );

If you are using a custom Tasty Recipes PHP template, add the following snippet where you would like the ratings to appear:

<?php if ( ! empty( $recipe_rating_icons ) || ! empty( $recipe_rating_label ) ) : ?>
	<div class="tasty-recipes-rating">
		<?php if ( ! empty( $recipe_rating_icons ) ) : ?>
			<p><?php echo $recipe_rating_icons; ?></p>
		<?php endif; ?>
		<?php if ( ! empty( $recipe_rating_label ) ) : ?>
			<p><?php echo $recipe_rating_label; ?></p>
		<?php endif; ?>
	</div>
<?php endif; ?>

As a note, ratings that exist in your recipes already (either natively with Tasty Recipes or in recipes to be converted) will not be transferred over to the WP-PostRatings functionality. These ratings are stored in your database and will be used if the default rating system is reactivated.

Was this article helpful?

Related Articles

Need Support?

Can't find the answer you're looking for?
Contact Support