First , we make custom Query Option.
add_filter( 'bricks/setup/control_options', function( $control_options ) {
$control_options['queryTypes']['google_review'] = esc_html__( 'Google Review', 'my-plugin' );
return $control_options;
} );
add_filter( 'bricks/query/run', function( $results, $query_obj ) {
if ( $query_obj->object_type !== 'google_review' ) {
return $results;
}
$api_key = ''; // Fill this with google place api
$place_id = '';
// Google Places API endpoint for fetching place details
$url = 'https://maps.googleapis.com/maps/api/place/details/json?place_id=' . $place_id . '&key=' . $api_key;
// Make request to Google Places API
$response = wp_remote_get($url);
// Check if request was successful
if (is_wp_error($response)) {
return 'Error fetching reviews: ' . $response->get_error_message();
} else {
// Decode JSON response
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (isset($data['status']) && $data['status'] == 'OK' && isset($data['result']['reviews'])) {
$reviews = $data['result']['reviews'];
// Prepare reviews array
$formattedReviews = [];
foreach ($reviews as $review) {
$formattedReviews[] = [
'author_name' => $review['author_name'],
'rating' => $review['rating'],
'text' => $review['text'],
'profile_photo_url' => $review['profile_photo_url'],
'relative_time_description' => $review['relative_time_description']
];
}
return $formattedReviews;
} else {
// If no reviews found, return default empty reviews
return 'no data';
}
}
}, 10, 2 );
add_filter( 'bricks/query/loop_object', function( $loop_object, $loop_key, $query_obj ) {
if ( $query_obj->object_type !== 'google_review' ) {
return $loop_object;
}
/* Usually you'd prepare a WP_Post object or something
* in here (see official documentation).
* In this specific case there is nothing to do here.
*/
return $loop_object;
}, 10, 3 );
function get_data_from_google_review( $name ) {
$loop_object = \Bricks\Query::get_loop_object();
if ( ! $loop_object ) return false;
if ( ! is_array( $loop_object ) ) return false;
if ( ! array_key_exists( $name, $loop_object ) ) return false;
return $loop_object[$name];
}
Allow the code to be executed in Bricks Builder by using this code below:
add_filter( 'bricks/code/echo_function_names', function() {
return [
'get_data_from_google_review',
];
} );Then for displaying data use this function insde dynamic data field.
echo:get_data_from_google_review('author_name')
echo:get_data_from_google_review('rating')