Dynamic stars rating from plugin for Bricks rating element

Hi

in case seomebody wants to use the new Bricks rating element together with the rating plugin

kk Star Ratings

so that you can read the ratings from the plugin but are free to show different icons, color, sizes, etc., here is the code for that.

Put this code into functions.php or a code snippet plugin:

function get_stars_rating() {
        $best  = get_option('kksr_stars');
        $score = get_post_meta(get_the_ID(), '_kksr_ratings', true) ? ((int) get_post_meta(get_the_ID(), '_kksr_ratings', true)) : 0;
        $votes = get_post_meta(get_the_ID(), '_kksr_casts', true) ? ((int) get_post_meta(get_the_ID(), '_kksr_casts', true)) : 0;
        $avg = $score && $votes ? round((float)(($score/$votes)*($best/5)), 1) : 0;
    return $avg;
}    

Add to or create new the White list for the get_stars_rating function in the functions.php with this

add_filter( 'bricks/code/echo_function_names', function() {
  return [
  	  'get_stars_rating',
  ];
} );

Now you can add the function into the rating field of the Bricks rating element like this:

stars1

Example output:

stars2
stars3

Cheers

Patric

1 Like

Hi

if you want to use a dynamic data tag {kk_star-rating} for the rating, you can use this code:

add_filter( 'bricks/dynamic_tags_list', 'add_starrating_tag_to_builder' );
function add_starrating_tag_to_builder( $tags ) {
  // Ensure your tag is unique (best to prefix it)
  $tags[] = [
    'name'  => '{kk_star-rating}',
    'label' => 'kk Star Rating',
    'group' => 'My Dynamic Data Group',
  ];

  return $tags;
}
add_filter( 'bricks/dynamic_data/render_tag', 'get_starrating_tag_value', 20, 3 );
function get_starrating_tag_value( $tag, $post, $context = 'text' ) {
  // $tag is the tag name with the curly braces after priority 10
  // Only look for dynamic tag my_dd_tag
  if ( $tag !== '{kk_star-rating}' ) {
    return $tag;
  }

  // Do your custom logic here, you should define run_my_dd_tag_logic() function
  $value = run_rating_dd_tag_logic();

  return $value;
}

 function run_rating_dd_tag_logic() {
  // Do your custom logic here
  
  $best  = get_option('kksr_stars');
  $score = get_post_meta(get_the_ID(), '_kksr_ratings', true) ? ((int) get_post_meta(get_the_ID(), '_kksr_ratings', true)) : 0;
  $votes = get_post_meta(get_the_ID(), '_kksr_casts', true) ? ((int) get_post_meta(get_the_ID(), '_kksr_casts', true)) : 0;
  $avg = $score && $votes ? round((float)(($score/$votes)*($best/5)), 1) : 0;
  $my_value = $avg;
  return $my_value;
}

add_filter( 'bricks/dynamic_data/render_content', 'render_kkstar_rating', 20, 3 );
add_filter( 'bricks/frontend/render_data', 'render_kkstar_rating', 20, 2 );
function render_kkstar_rating( $content, $post, $context = 'text' ) {

  // $content might consists of HTML and other dynamic tags
  // Only look for dynamic tag {my_dd_tag}
  if ( strpos( $content, '{kk_star-rating}' ) === false ) {
    return $content;
  }
 
  // Do your custom logic here, you should define run_my_dd_tag_logic() function
  $my_value = run_rating_dd_tag_logic();
 
  // Replace the tag with the value you want to display
  $content = str_replace( '{kk_star-rating}', $my_value, $content );

  return $content;
}

Put this into the functions.php file or use a code snippet plugin.

Then you can just use the rating field like this:

stars4

Cheers

Patric

3 Likes