Manually add rating block with PHP

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.

6
1
1620
Rate this article

You are not allowed to rate this post.

2 Comments

  1. SiyahBeyaz says:

    How can we translate the text?

    1. MillaN says:

      The plugin supports translations system in WordPress, and you can create PO/MO translation files to use. POT file with all strings is in the plugin root.

Leave a Reply