Manually add rating block with PHP

Developer Knowledge Level

This content is intended for WordPress developers, and it may require coding knowledge of WordPress, PHP, and JavaScript. Code examples provided here may contain errors or needs some additional coding. Make sure to test the code before using it on a live website!

The plugin has few ways to add rating blocks into the website. But, if that is not enough and you want to manually position rating blocks and set them differently, you can use PHP functions to do just that.

Intro

Before we go on, make sure you understand basics of GD Rating System objects and terminology:

To get parameters for different rating methods to use with these functions, check out the articles about each rating method

Main function

Function gdrts_render_rating is the main rendering function. This function can render rating block for any rating item and any rating method.

Function syntax

gdrts_render_rating($args = array(), $method = array())

  • $args – list of settings related to rating item.
  • $method – list of settings related to rating method, and depends on the rating method set with $args.

$args is array with following elements:

  • echo: display or return rendered rating block. Default: false.
  • entity: rating type entity.
  • name: rating type name.
  • id: rating item real ID.
  • item_id: rating item ID from ratings table, assigned once when rating item is first requested and added to database.
  • method: rating method: stars-rating, thumbs-rating, like-this or stars-review.

If you specify item_id, you don’t need entity, name and id.

$method is array with elements depending on the method from $args array. You can check out individual rating methods articles for list of parameters.

Examples

Here are few examples for this function.

Post with ID 10, Stars Rating method, default rendering settings
gdrts_render_rating(array('echo' => true, 'entity' => 'posts', 'name' => 'post', 'id' => 10));

This will display rating block, and all stars rating settings will be taken from plugin settings and custom rules settings.

Page with ID 5, Stars Review method, custom rendering settings
gdrts_render_rating(
  array('echo' => true, 'entity' => 'posts', 'name' => 'page', 'id' => 5, 'method' => 'stars-review'),
  array('template' => 'expanded', 'style_size' => 40)
);

This will display rating block, and stars rating settings will be taken from plugin settings and custom rules settings. Only template and stars size will be set by this function.

Posts specific functions

This function works only for posts, pages and custom post types. It can also detect current loop post.

Function syntax

gdrts_posts_render_rating($args = array(), $method = array())

  • $args – list of settings related to rating item.
  • $method – list of settings related to rating method, and depends on the rating method set with $args.

$args is array with following elements:

  • echo: display or return rendered rating block. Default: false.
  • name: post type.
  • id: post real ID.
  • item_id: rating item ID from ratings table, assigned once when rating item is first requested and added to database.
  • method: rating method: stars-rating, thumbs-rating or stars-review.

If you specify item_id, you don’t need name and id. If you don’t specify name and id, and you don’t specify item_id, plugin will take the current post from the WordPress loop.

$method is array with elements depending on the method from $args array. You can check out individual rating methods articles for list of parameters.

Examples

Here are few examples for this function.

Post with ID 10, Stars Rating method, default rendering settings
gdrts_posts_render_rating(array('echo' => true, 'name' => 'post', 'id' => 10));

This will display rating block, and all stars rating settings will be taken from plugin settings and custom rules settings.

Current post from loop, Stars Rating method, custom template
gdrts_posts_render_rating(
  array('echo' => true),
  array('template' => 'light')
);

This will display rating block by taking current post from loop, and stars rating settings will be taken from plugin settings and custom rules settings. Only templatewill be set by this function.

Current post from loop, Stars Rating method, all defaults into variable
$rating = gdrts_posts_render_rating();

This is minimal function call, it doesn’t display rating block, it places it into variable, detects post from loop, and uses all default settings for Stars Rating.

Current post from loop, Stars Rating method
$rating = gdrts_posts_render_rating(array('echo' => true));

This is minimal function call, it displays rating block, detects post from loop, and uses all default settings for Stars Rating.

8
2
5087
Rate this article

You are not allowed to rate this post.

7 thoughts on “Manually add rating block with PHP”

  1. This code shows the ratings even if the user has chosen hide in the “Display Rating Block” dropdown when creating the post. How can we add the ratings using custom code while observing the users selected option?

Leave a Comment