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 voting process is controlled by the global plugin settings or settings rules for every rating method. Until there is an interface to allow modifications for the individual rating items, the only way to control who is allowed to vote for specific items is to use custom code and control the vote rules based on any criteria you want to use.

Each rating method has its own mail ‘calc’ filter that runs before the rating block is displayed. This example here is for the Stars Rating method, and the filter used for each method is different and listed at the end of this article.

The Function

Inside the function hook to the filter, we can change anything coming through in the $calc array. Still, for this article, we are interested in only one key: ‘allowed.’ If the current user/visitor is allowed to vote (based on the global and rules settings), this array value will be TRUE; if not, it will be FALSE. You can modify this value using your own reasons (maybe interface with your customer’s system to allow voting to customers of the specific items only).

The code displayed here includes some comments to understand what data you can use inside the function. And line 17, the function checks if the rating item is the post type ‘post.’ If yes, you can run other checks and make the change to ‘allowed.’ Inside that block, you can use $user_id to run through some other custom code to determine if that user is a customer or not.

add_filter( 'gdrts_stars_rating_loop_single_calc', 'gdrts__gdrts_stars_rating_loop_single_calc', 10, 1 );
function gdrts__gdrts_stars_rating_loop_single_calc( $calc ) {
  $item    = gdrts_single()->item();
  $user_id = get_current_user_id();

  /**
   * inside this function, you can use following variables:
   *
   * $calc    - array with the current rating settings and values
   * $item    - current rating item
   * $user_id - current user ID */

  /**
   * now, use the variables above and make the determination if the user can vote or not.
   * for this example, we will change if the rating item is a 'post' first. */

  if ( $item->entity == 'posts' && $item->name == 'post' ) {
    $post_id = $item->id;

    /**
     * include any other conditions to determine if the 'allowed' has to be changed */

    $calc['allowed'] = false;
  }

  return $calc;
}

Also, you can use $post_id inside that block, and You will set it to the actual ID of the post.

Filter for other Methods

Each method has a different filter:

  • Stars Rating: gdrts_stars_rating_loop_single_calc
  • Thumbs Rating: gdrts_thumbs_rating_loop_single_calc
  • Slider Rating: gdrts_slider_rating_loop_single_calc
  • Emote This: gdrts_emote_this_loop_single_calc
  • Like This: gdrts_like_this_loop_single_calc
0
0
68
Rate this article

You are not allowed to rate this post.

Comments

Leave a Reply