If you have custom code which relies on the Multi Rating Pro plugin and the plugin is disabled, a PHP fatal error will occur. It is best practice to first check the plugin is installed and activated before calling any API functions from a plugin. See class_exists() or function_exists() PHP functions.

<?php
$rating_form_id = 1;
if ( class_exists( 'MRP_Multi_Rating_API' ) ) {
$rating_form = MRP_Multi_Rating_API::get_rating_form( $rating_form_id );
// do something
}

Table of Contents

  1. Search Form
  2. Add ” ratings)” text to the end of the count
  3. Refresh Rating Item Results
  4. Increase font size of stars or thumbs
  5. Change the JSON-LD structured data (rich snippets)
  6. Custom WP Query Sort
  7. Automatically approve ratings > 3 out of 5 stars
  8. Check if user rating exists
  9. Rearrange comment form fields

Search Form

The code snippet below allows the user to select an overall star rating as search criteria in the search form. The star ratings are rounded half up or half down e.g. an overall star rating of 4.67/5 will show up in search results for 5 stars.

<?php
/**
* Search results are rounded half up or half down e.g. an overall rating of 4.67 will show up in
* search results for 5 stars.
*
* @param unknown $where
* @param unknown $query
* @return string
*/
function mrp_search_posts_where( $where, $query ) {
if ( isset( $_REQUEST['mrp-search'] ) && ! is_admin() && $query->is_search && $query->is_main_query() ) {
$search = intval( $_REQUEST['mrp-search'] );
$rating_form_id = 1;
$filters_hash = MRP_Multi_Rating_API::get_filters_hash( array() ); // if you want to add any Multi Rating pro specific filters e.g. rating_item_ids
$where .= ' AND rr.rating_form_id = ' . $rating_form_id . ' AND rr.filters_hash = "' . $filters_hash . '"'
. ' AND ROUND( rr.adjusted_star_result ) = ' . $search;
}
return $where;
}
add_filter( 'posts_where' , 'mrp_search_posts_where', 10, 2 );
/**
*
* Joins Multi Rating Pro tables for search form
*
* @param unknown $join
* @param unknown $query
* @return string
*/
function mrp_search_posts_join( $join, $query ) {
if ( isset( $_REQUEST['mrp-search'] ) && ! is_admin() && $query->is_search && $query->is_main_query() ) {
global $wpdb;
$join .= ' LEFT JOIN ' . $wpdb->prefix . MRP_Multi_Rating::RATING_RESULT_TBL_NAME . ' rr ON ' . $wpdb->posts . '.ID'
. ' = rr.post_id AND rr.rating_item_id IS NULL AND rr.rating_entry_id IS NULL';
}
return $join;
}
add_filter('posts_join', 'mrp_search_posts_join', 10, 2);
/**
* The template for displaying search forms
*
* TODO: Copy the code below and put it in a file called searchform.php in your theme.
*/
?>
<form method="get" id="searchform" action="<?php bloginfo('url'); ?>/">
<div>
<input type="text" value="<?php the_search_query(); ?>" name="s" id="s" />
<label for="mrp-search" style="margin-top: 5px; margin-bottom: 5px;"><?php _e( 'Overall Star Rating' ); ?></label>
<select name="mrp-search" style="margin-top: 5px; margin-bottom: 5px;">
<option value="0"><?php _e( 'No stars' ); ?></option>
<option value="1"><?php _e( '1 star' ); ?></option>
<option value="2"><?php _e( '2 stars' ); ?></option>
<option value="3"><?php _e( '3 stars' ); ?></option>
<option value="4"><?php _e( '4 stars' ); ?></option>
<option value="5"><?php _e( '5 stars' ); ?></option>
</select>
<input type="submit" id="searchsubmit" value="Search" />
</div>
</form>

Add ” ratings)” text to the end of the count

Changes the count of rating entries displayed alongside the rating results to be “(X ratings)” instead of “(X)”.

<?php
/**
* after count e.g. " ratings)"
* @param string $after_count
*/
function mrp_rating_result_after_count( $after_count ) {
return __( ' ratings)' );
}
add_filter( 'mrp_rating_result_after_count', 'mrp_rating_result_after_count' );
/**
* before count e.g. "("
* @param string $after_count
*/
function mrp_rating_result_before_count( $before_count ) {
// default is "("
return $before_count;
}
add_filter( 'mrp_rating_result_before_count', 'mrp_rating_result_before_count' );

Refresh Rating Item Results

By default, when a rating is saved, updated or deleted, only the rating results for the specific post and rating form are recalculated. If you are using the the [mrp_rating_item_results] shortcode or the rating_item_ids shortcode attribute (e.g. [mrp_rating_result rating_item_ids="1,2"]), the associated rating results are recalculated on page load (not immediately when a rating is saved, updated or deleted).

<?php
/**
* Recalculate rating item results for a post and rating form
*
* @param unknown $post_id
* @param unknown $rating_form_id
*/
function mrp_recalculate_rating_item_results( $post_id, $rating_form_id ) {
$rating_form = MRP_Multi_Rating_API::get_rating_form( $rating_form_id );
$rating_items = MRP_Multi_Rating_API::get_rating_items( array( 'rating_form_id' => $rating_form_id ) );
// Lets recalculate rating item results for this specific post and rating form
foreach ( $rating_items as $rating_item ) {
$rating_item_result = MRP_Multi_Rating_API::get_rating_item_result( array(
'post_id' => $post_id,
'rating_form_id' => $rating_form_id,
'rating_item' => $rating_item
) );
}
}
function my_mrp_after_delete_rating_entry_success( $params ) {
// params = array( 'rating_entry_id' => $rating_entry_id, 'post_id' => $post_id, 'rating_form_id' => $rating_form_id )
mrp_recalculate_rating_item_results( $params['post_id'], $params['rating_form_id'] );
}
function my_mrp_after_save_rating_entry_success( $rating_entry, $is_new, $entry_status_changed ) {
mrp_recalculate_rating_item_results( $rating_entry['post_id'], $rating_entry['rating_form_id'] );
}
add_action( 'mrp_after_delete_rating_entry_success', 'my_mrp_after_delete_rating_entry_success', 10, 1 );
add_action( 'mrp_after_save_rating_entry_success', 'my_mrp_after_save_rating_entry_success', 10, 3 );

Increase font size of stars or thumbs

Font Awesome is used for the star and thumbs icons. To increase the size of the star and thumb icons you can simply increase the font-size using some CSS.

.mrp-star-rating, .mrp-thumbs {
font-size: 150%;
}

Change the JSON-LD structured data (rich snippets)<

See Google SEO rich snippets on how to setup. Here’s some code samples:

<?php
/**
* Adds example address, priceRange and telephone data to the LocalBusiness type
*/
function mrp_structured_data_type_localbusiness_example( $schema, $post_id ) {
$structured_data_type = get_post_meta( $post_id, MRP_Multi_Rating::STRUCTURED_DATA_TYPE_POST_META, true );
if ( $structured_data_type === 'LocalBusiness' ) {
return '
"address": {
"@type": "PostalAddress",
"addressLocality": "Seattle",
"addressRegion": "WA",
"postalCode": "98052",
"streetAddress": "20341 Whitworth Institute 405 N. Whitworth"
},
"priceRange": "$$$",
"telephone": "+18005551234",';
}
return '';
}
add_filter( 'mrp_structured_data_type' , 'mrp_structured_data_type_localbusiness_example', 10, 2);

<?php
/**
* Adds example mainEntityofPage to Product type based on WordPress SEO graph
*/
function mrp_structured_data_type_product_main_entity_example( $schema, $post_id ) {
$structured_data_type = get_post_meta( $post_id, MRP_Multi_Rating::STRUCTURED_DATA_TYPE_POST_META, true );
if ( $structured_data_type === 'Product' ) {
return '
"mainEntityOfPage": "' . get_the_permalink( $post_id ) . '#webpage",
';
}
return '';
}
add_filter( 'mrp_structured_data_type' , 'mrp_structured_data_type_product_main_entity_example', 10, 2);

Custom WP Query Sort

<?php
$query = new WP_Query( array( 'mrp_sort_highest_rated' => true ) );
/**
* Adds WP query sort by highest rating then count entries
*
* @param unknown $order_by
* @param unknown $query
* @return string
*/
function mrp_posts_orderby( $order_by, $query ) {
if ( $query->get( 'mrp_sort_highest_rated' ) == true ) {
$order_by = 'adjusted_star_result DESC, count_entries DESC'; // highest rated
}
return $order_by;
}
add_filter( 'posts_orderby','mrp_posts_orderby', 10, 2 );
/**
* WP query join tables for sorting by ratings
*
* @param unknown $join
* @param unknown $query
* @return string
*/
function mrp_posts_join( $join,$query ) {
global $wpdb;
if ( $query->get( 'mrp_sort_highest_rated' ) == true ) {
$general_settings = (array) get_option( MRP_Multi_Rating::GENERAL_SETTINGS );
$rating_form_id = $general_settings[MRP_Multi_Rating::DEFAULT_RATING_FORM_OPTION]; // you could remove this from the join if you want
$filters_hash = MRP_Multi_Rating_API::get_filters_hash( array() ); // if you want to add any Multi Rating pro specific filters e.g. rating_item_ids
$join .= ' LEFT JOIN ' . $wpdb->prefix . MRP_Multi_Rating::RATING_RESULT_TBL_NAME . ' rr ON ' . $wpdb->posts . '.ID'
. ' = rr.post_id AND rr.rating_form_id = ' . $rating_form_id . ' AND rr.filters_hash = "' . $filters_hash
. '" AND rr.rating_item_id IS NULL AND rr.rating_entry_id IS NULL';
}
return $join;
}
add_filter('posts_join', 'mrp_posts_join', 10, 2);

Automatically approve ratings > 3 out of 5 stars

<?php
/**
* Automatically approve ratings > 3 out 5 stars.
*/
function my_auto_approve_good_ratings( $auto_approve_ratings, $rating_entry ) {
$rating_result = mrp_calculate_rating_entry_result( $rating_entry );
if ( $rating_result['adjusted_star_result'] > 3 ) {
return true;
}
return false;
}
add_filter( 'mrp_auto_approve_ratings', 'my_auto_approve_good_ratings', 10, 2 );

Check if user rating exists

<?php
// get post id - assumes in WP loop
global $post;
$post_id = $post->ID;
// get default rating form for post, this checks the filter settings, post meta and general settings
$rating_form_id = MRP_Utils::get_rating_form( $post_id );
// get current logged in user
$user_id = 0; // anonymous
$user = wp_get_current_user();
if ( $user && $user->ID ) {
$user_id = $user->ID;
}
// show rating form is not logged in or user has not yet rated, otherwise show rating item
// results in option block layout
if ( $user_id == 0 || ! MRP_Multi_Rating_API::user_rating_exists( $rating_form_id, $post_id, $user_id ) ) {
mrp_rating_form();
} else {
mrp_rating_item_results( array( 'layout' => 'options_block' ) );
}

Rearrange comment form fields

This code sample rearranges the comment form fields for anonymous users. It moves the comment, author, email and url fields to the end of the form. Therefore, any rating items, review fields or custom fields will be shown first in the comment form. Note the code sample relies on the comment_form_fields filter which is only available since WordPress 4.4.

<?php
/**
* Changes order of comment form fields for anonymous users. Rating form items first, followed
* by comment, author, email and url. Note the comment_form_fields filter is only available since
* WordPress 4.4
*
* @param array $fields
* @return array $fields
*/
function my_comment_form_fields( $fields ) {
$comment = $fields['comment'];
unset( $fields['comment'] );
array_push( $fields, $comment );
$author = $fields['author'];
unset( $fields['author'] );
array_push( $fields, $author );
$email = $fields['email'];
unset( $fields['email'] );
array_push( $fields, $email );
$url = $fields['url'];
unset( $fields['url'] );
array_push( $fields, $url );
return $fields;
}
add_filter( 'comment_form_fields', 'my_comment_form_fields', 100, 1 );