https://codewp.ai/snippet/6herwuh5divtkvqlvkebirbxymu26/
Copy URL

Share this on your favorite social networks:

OR
Please log in to use this feature.
Email To Yourself Or A Developer
verified
Verified working as of March 21, 2023
GenID: 1637

Shortcode that gets and displays reviews from Google Reviews API

A shortcode that gets and shows reviews for a Google Place. Requires a Maps API key, and a Place ID. Get the API key here, and the lookup the place ID here. This is shortcode gets and renders the HTML, but it's up to you to style the output as there's no CSS included here.

Original
function get_reviews() {
	$api_key = 'AIzaSyB-Q_Q-Q_Q-Q_Q-Q_Q-Q_Q-Q_Q-Q_Q';
	$place_id = 'ChIJQ_Q-Q_Q-Q_Q-Q_Q-Q_Q-Q_Q-Q_Q';
	$url = 'https://maps.googleapis.com/maps/api/place/details/json?placeid=' . $place_id . '&key=' . $api_key;
	$json = file_get_contents($url);
	$obj = json_decode($json);
	$reviews = $obj->result->reviews;
	$review_count = count($reviews);
	$review_html = '';
	for ($i = 0; $i < $review_count; $i++) {
		$review_html .= '<div class="review">';
		$review_html .= '<div class="review-author">';
		$review_html .= '<img src="' . $reviews[$i]->profile_photo_url . '" alt="' . $reviews[$i]->author_name . '" />';
		$review_html .= '<span class="review-author-name">' . $reviews[$i]->author_name . '</span>';
		$review_html .= '</div>';
		$review_html .= '<div class="review-rating">';
		$review_html .= '<span class="review-rating-stars">';
		for ($j = 0; $j < $reviews[$i]->rating; $j++) {
			$review_html .= '<i class="fa fa-star"></i>';
		}
		$review_html .= '</span>';
		$review_html .= '<span class="review-rating-date">' . date('F j, Y', strtotime($reviews[$i]->time)) . '</span>';
		$review_html .= '</div>';
		$review_html .= '<div class="review-text">' . $reviews[$i]->text . '</div>';
		$review_html .= '</div>';
	}
	return $review_html;
}
add_shortcode('get_reviews', 'get_reviews');

Snippet Explanation

This code defines a function named "get_reviews" which uses the Google Places API to retrieve reviews for a specified place and generates HTML markup to display them. The function accepts no parameters and returns a string containing the HTML markup.

The code uses the "file_get_contents" function to retrieve JSON data from the Google Places API, which includes reviews for a specified place ID. It then parses the JSON data using the "json_decode" function and extracts the review data from the result.

The function then loops through the reviews and generates HTML markup for each review, including the author name, profile photo, rating, date, and review text. The generated HTML markup is stored in the "$review_html" variable, which is then returned by the function.

Finally, the code uses the "add_shortcode" function to register the "get_reviews" function as a shortcode, which can be used in WordPress posts or pages to display the reviews by calling the shortcode like this: [get_reviews].

Read More
publishLoad Prompt
preview
Make Public
share
Share Snippet
download
Export Snippet
Mode: WordPress PHP
Language: PHP
Tags:
Visibility: Public
Date:
November 28, 2022
Creator:
@James LePa...
cross