geminilabs

[site-reviews] shortcode [site_review]

Nov 12th, 2020 (edited)
1,392
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /**
  2.  * Registers the [site_review] shortcode
  3.  * This shortcode displays a single review
  4.  *
  5.  * Shortcode options are as follows:
  6.  * - [site_reviews form=""] Optional. Allowed values: the Post ID of a custom review form
  7.  * - [site_reviews hide=""] Optional. Allowed values are same as in the [site_reviews] shortcode.
  8.  * - [site_reviews post_id=""] Optional. Allowed values: the Post ID of a review
  9.  * - [site_reviews theme=""] Optional. Allowed values: the Post ID of a custom review theme
  10.  *
  11.  * Paste this in your active theme's functions.php file
  12.  * @param array $atts
  13.  * @return string|void
  14.  */
  15. add_shortcode('site_review', function ($atts) {
  16.     $atts = wp_parse_args($atts, [
  17.         'form' => '',
  18.         'hide' => '',
  19.         'post_id' => '',
  20.         'theme' => '',
  21.     ]);
  22.     if (!empty($atts['post_id'])) {
  23.         $reviews = apply_filters('glsr_get_reviews', null, [
  24.             'per_page' => 1,
  25.             'post__in' => (array) $atts['post_id'],
  26.         ]);
  27.     }
  28.     if (!empty($reviews)) {
  29.         $reviews->args['form'] = $atts['form'];
  30.         $reviews->args['hide'] = array_map('trim', explode(',', $atts['hide']));
  31.         $reviews->args['theme'] = $atts['theme'];
  32.         $html = $reviews->build();
  33.         return sprintf('<div class="%s">%s</div>', $html->style, $html);
  34.     }
  35. });
RAW Paste Data Copied