geminilabs

[site-reviews] [site_reviews_rating] shortcode

May 19th, 2020
430
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.30 KB | None | 0 0
  1. /**
  2.  * Registers the [site_reviews_rating] shortcode
  3.  * This shortcode displays the average star rating of a page or category that has reviews assigned to them.
  4.  *
  5.  * Shortcode options are as follows:
  6.  * - [site_reviews_rating category=""] Optional. Allowed values: category slug or ID
  7.  * - [site_reviews_rating assigned_to=""] Optional. Allowed values: post/page ID or "post_id" to use the current page ID
  8.  *
  9.  * Note: the "assigned_to" option will take precedence over the "category" option, you should only use one or the other.
  10.  *
  11.  * Paste this in your active theme's functions.php file
  12.  * @param array $atts
  13.  * @return int
  14.  */
  15. add_shortcode('site_reviews_rating', function ($atts) {
  16.     $atts = wp_parse_args($atts, [
  17.         'assigned_to' => '',
  18.         'category' => '',
  19.     ]);
  20.     $rating = 0;
  21.     if (!empty($atts['category'])) {
  22.         if ($term = term_exists($atts['category'], 'site-review-category')) {
  23.             $rating = get_term_meta($term['term_id'], '_glsr_average', true);
  24.         }
  25.     }
  26.     if (!empty($atts['assigned_to'])) {
  27.         $postId = $atts['assigned_to'] == 'post_id'
  28.             ? get_the_ID()
  29.             : $atts['assigned_to'];
  30.         $rating = get_post_meta($postId, $metaKey, true);
  31.     }
  32.     return apply_filters('glsr_star_rating', $rating, $rating);
  33. });
Add Comment
Please, Sign In to add comment