Recommended Template for Google Reviews

[ad_1]

Hey Everyone,

Working on a way to import google reviews into our wordpress site WITHOUT a plugin. We need reviews for multiple stores on the same wordpress site so all the freemium plugins are not an option if we want to stay free. Considering the simplicity of the build I refuse to turn this into a monthly subscription when I can just setup a PHP template instead or even pay a developer a one time fee.

ChatGPT gave me this template below, but I’m wondering if you know of a better template that already exists on github or somewhere similar that you could share so I could apply it to my website.

Appreciate your help!

`<?php`

`// Function to retrieve Google My Business reviews`

`function get_google_reviews($api_key, $place_id, $num_reviews = 5) {`

`$url = “https://maps.googleapis.com/maps/api/place/details/json?placeid=$place_id&key=$api_key”;`

`$response = wp_remote_get($url);`

`if (is_wp_error($response)) {`

`return false;`

`}`

`$body = wp_remote_retrieve_body($response);`

`$data = json_decode($body, true);`

`if (!isset($data[‘result’][‘reviews’])) {`

`return false;`

`}`

`$reviews = array_slice($data[‘result’][‘reviews’], 0, $num_reviews);`

`return $reviews;`

`}`

`// Function to display Google My Business reviews`

`function display_google_reviews($api_key, $place_id, $num_reviews = 5) {`

`$reviews = get_google_reviews($api_key, $place_id, $num_reviews);`

`if (!$reviews) {`

`echo ‘No reviews found.’;`

`return;`

`}`

`echo ‘<div class=”google-reviews”>’;`

`echo ‘<h2>Google Reviews</h2>’;`

`echo ‘<ul>’;`

`foreach ($reviews as $review) {`

`echo ‘<li>’;`

`echo ‘<strong>’ . esc_html($review[‘author_name’]) . ‘</strong>’;`

`echo ‘<p>’ . esc_html($review[‘text’]) . ‘</p>’;`

`echo ‘</li>’;`

`}`

`echo ‘</ul>’;`

`echo ‘</div>’;`

`}`

`// Usage: Replace ‘YOUR_API_KEY’ and ‘YOUR_PLACE_ID’ with your actual API key and place ID`

`$api_key = ‘YOUR_API_KEY’;`

`$place_id = ‘YOUR_PLACE_ID’;`

`$num_reviews = 5;`

`// Display Google My Business reviews`

`display_google_reviews($api_key, $place_id, $num_reviews);`

`?>`

[ad_2]
1 Comment

 

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