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’,
];
});
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;
});
?>