geminilabs

[site-reviews] set rating number dynamically from custom rating fields

Oct 26th, 2020 (edited)
943
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Dynamically set the rating to the average of multiple custom rating fields.
  3.  * !IMPORTANT: make sure to hide the rating field in the form for this to work.
  4.  * Paste this in your active theme's functions.php file.
  5.  * @param array $values
  6.  * @return array
  7.  */
  8. add_filter('site-reviews/create/review-values', function ($values) {
  9.     $ratings = [
  10.         glsr_get($values, 'custom.storyline_rating', 0),
  11.         glsr_get($values, 'custom.graphics_rating', 0),
  12.         glsr_get($values, 'custom.music_rating', 0),
  13.         glsr_get($values, 'custom.presentation_rating', 0),
  14.     ];
  15.     $ratingsCount = count($ratings);
  16.     $ratingsSum = array_sum($ratings);
  17.     $values['rating'] = round($ratingsSum / $ratingsCount);
  18.     return $values;
  19. });
RAW Paste Data