Array merge errors attempting to add Site Reviews dynamic data to Bricks rating/review

I’m trying to do something similar to what Patric did in this post: Dynamic stars rating from plugin for Bricks rating element

I’m using the Site Reviews plugin. I basically revamped his code with Site Reviews data, and whitelisted the Bricks function. After several failed attempts, I isolated it to just testing this, and it throws the same error, so the error seems to be with Bricks. Any ideas on how to fix?

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

Well, to understand what is wrong, we would need to see your revamped code.

Best regards

Patric

I tried a bunch, courtesy of GPT (I am by no means a coder). Here are a few:

<?php
function get_stars_rating() {
    global $wpdb;

    // Get the current post ID
    $post_id = get_the_ID();

    // Query the database for the average rating
    $result = $wpdb->get_row($wpdb->prepare("
        SELECT AVG(rating) AS avg_rating
        FROM wp_glsr_ratings
        WHERE review_id = %d AND is_approved = 1
    ", $post_id));

    // Return the average rating rounded to 1 decimal, or 0 if no ratings exist
    return isset($result->avg_rating) ? round($result->avg_rating, 1) : 0;
}

// Add this function to Bricks' dynamic data whitelist
add_filter('bricks/code/echo_function_names', function ($functions) {
    $functions[] = 'get_stars_rating';
    return $functions;
});
?>

<?php
// Function to calculate the average rating
function get_stars_rating() {
    global $wpdb;

    // Get the current post ID
    $post_id = get_the_ID();

    // Query the wp_glsr_ratings table for average rating
    $result = $wpdb->get_row($wpdb->prepare("
        SELECT AVG(rating) as avg_rating, COUNT(rating) as total_reviews
        FROM wp_glsr_ratings
        WHERE review_id = %d AND is_approved = 1
    ", $post_id));

    // If there are no reviews, return 0
    if (!isset($result->avg_rating) || !$result->avg_rating) {
        return 0;
    }

    return round($result->avg_rating, 1); // Return the average rating rounded to 1 decimal
}

<?php // First, define the get_stars_rating function function get_stars_rating() { global $wpdb; $post_id = get_the_ID(); $result = $wpdb->get_row($wpdb->prepare(" SELECT AVG(rating) as avg_rating FROM wp_glsr_ratings WHERE review_id = %d AND is_approved = 1 ", $post_id)); return isset($result->avg_rating) ? round($result->avg_rating, 1) : 0; } // Then add the whitelist filter you already have add_filter('bricks/code/echo_function_names', function($function_names) { if (!is_array($function_names)) { $function_names = []; // Initialize as empty array } $function_names[] = 'get_stars_rating'; return $function_names; }); ?>

Did you get a chance to review my code? Thank you!

You can actually just get the code from your Bricks Settings page. (Code Review)

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

Thanks. I tried that but still get the array errors.

Hi,

We can’t replicate the issue.

You can use your purchased license email to send admin credentials to help@bricksbuilder.io (include this forum URL as a reference)

I will help to check. Please also let us know the page URL and location of element that use this function as well.

Regards,
Jenn

Thank you. Will send an email shortly!