Random Review Shortcode: I had to modify the pastebin code

[ad_1]

I tried to use the code from https://pastebin.com/naUae8zB to get a random review shortcode.

  • I created a child theme and pasted the code into functions.php.
  • Then I tried to use the shortcode [site_review_random].

Expected:
To get a random review.

Current:
Every time I added [site_review_random] anywhere WordPress threw an error in the area where the review was supposed to appear. I can’t really say what went wrong, it might be because I’m on PHP 8.1.

Workaround/Fix
I got the code working by adding $atts = []; in line 14 like so:

add_shortcode('site_review_random', function ($atts) {
    $atts = [];
    $atts['orderby'] = 'random';
    $atts['per_page'] = 1;
    if ($reviews = apply_filters('glsr_get_reviews', null, $atts)) {
        $html = $reviews->build();
        return sprintf('<div class="%s">%s</div>', $html->style, $html);
    }
});

I post this here so it might help others. Thank you for this nice plugin with such a flexible API and thank you, too, for the random review code.

The page I need help with: [log in to see the link]

 

This site will teach you how to build a WordPress website for beginners. We will cover everything from installing WordPress to adding pages, posts, and images to your site. You will learn how to customize your site with themes and plugins, as well as how to market your site online.

Buy WordPress Transfer