Add a Google Reviews Widget to WordPress Without a Plugin
This article will teach you how to leverage the Google Places API to add a Google reviews widget to your WordPress website.
Why Customer Reviews Matter
It doesn’t matter if you’re an online-only or a traditional brick-and-mortar business: customer reviews matter. Reviews are an important ranking signal for local search optimization (increasing the visibility of your business listings in services like Google My Business). In addition to getting visitors to your site, reviews provide trust and credibility that turns a casual visitor into a customer. In other words, they can influence click-through rates, bounce rates, and conversions.
If your website hosts it’s own customer reviews (in the right format), Google will show review stars as part of your organic listings.
But if you don’t have reviews built into your website, getting review stars to show up can be hard to achieve. And why add review functionality to your site when Google already provides them?
We’re going to leverage the reviews that are already on our Google My Business listings to add a reviews widget to a WordPress site.
Get a Google API Key
First, you’ll need a Google API key. Follow these instructions to create a project and create an API key, and then follow these instructions to enable the Google Places API for your new project:
- Go to your Google Developer Console and make sure you are on the app you just created.
- Click Enable API, search for the Google Places Web Service and click the link for that service.
- Click Enable to allow calls to the Google Places Web Service from your application.
This configuration will provide you with a key to access the Google API and query the Google Places web service.
Write WordPress Code to Pull Your Business’ Google Reviews
There is a little bit of code involved in this process, and I admittedly don’t write short code. So rather than reproduce it here, I’ve added it to a Gift which I’ve embedded below.
This code is split up into several sections.
The RWC_Google_Places class acts as a wrapper around the Google Places web services that we need to access.
Create a new instance by passing the API key to the class’ constructor. The findPlaces() method searches the Places API for all locations that match the query. The query can be a business name or address. The method returns a PHP object built from the JSON response retrieved from the API.
The getDetails() method queries the Places API for in-depth details about a particular “place” using the ID associated with the listing. Pass it a place ID, and it will return a PHP object containing the listing’s details.
The RWC_Google_Reviews class leverages the wrapper class to retrieve Place details and generate the badge via the WordPress Shortcode API. Create an instance by calling the constructor and passing your Google API key. It will automatically register shortcodes with WordPress.
Displaying the Google Reviews Widget
To generate a shortcode, add the No query or placeId attributes specified on shortcode. shortcode to your page content. Of course, that’s not quite enough. To create a badge, you’ll also have to specify one of two shortcode attributes: query or placeId. The query attribute can be used to send a query string, such as a business name or address, to Google Places, and it generates a badge for each match. If you happen to know your business’ placeId, you can use that instead. Examples:
[google-reviews-badge query="Reich Web Consulting"]
The code is long, but it’s not complicated. You can quickly reduce this code to just a couple of lines if you don’t care about things like comments and error-checking.
If you look closely at this code, it doesn’t load any styling. That’s intentional. The HTML output is pretty raw, and I decided to leave it up to anyone who decides to use this code to make their badge match their website.
So go ahead and give it a shot. Leave a comment with any questions or feedback about the code.