Since version 2.4.1, the testimonials API was made public allowing developers to extend it to other sources such as WordPress.org, Envato, Zomato, etc. Basically any platform that provides a public API for business data can be pulled and integrated into BNE Testimonials Pro. Let's take a look at each filter that is available.
- bne_testimonials_api_source_args - This filter allows adding your initial API call and arguments to the API source array. Some API's require authentication or other information to retrieve the information.
- bne_testimonials_api_source_response - This filter allows customizing the final response from the API request. In general, this makes it to where the correct data is assigned to the correct area of the shortcode output. At this time, a second request can be made if needed for other data not included in the initial request from bne_testimonials_api_source_args.
- bne_testimonials_api_fields - This filter allows customizing the final response(s) from the API request and direct the returned information to each are of the testimonial display (image, name, website, tagline, rating, date, message, etc.
- bne_testimonials_api_branding_assets - This filter returns an array that allows you to specify branding guidelines from your API source.
All of the above filters are found in /includes/api.php.
Zomato is an online restaurant directory and review source that is used around the world. To use Zomato also requires an API key from their developer portal. We will use each of the four filters listed above to integrate the Zomato into BNE Testimonials Pro.
API Source Args
First we need to add the new API into the initial source
$api_data array. This will be our initial request to the API and with Zomato, requires the user-key and listing ID. Both the
id are options within the shortcode, so they should be defined there.
What's going on... Because we're adding to the existing
$api_data array, we need to make sure we are only targeting the zomato source. So first we do a check for the source. Next, Zomato requires authentication which will be your API key and is added to
$api_data['api_remote_args']. This array handles the arguments used in the remote call with
wp_remote_get. Finally, we set the correct URL to query against for zomato. This URL includes the business
ID that is provided from the shortcode.
API Source Response
At this point we have received our first response from the API platform. In some cases we can be done with it and assign the information to
$testimonials. However, not everything can be that easy. Since we could be using the information in a number of ways either with the badge or a review list, not all information returned is available from the
restaurant endpoint. The
restaurant endpoint provides the business name, thumb, aggregate rating, total number of reviews and url. What's missing is the individual reviews.
Therefore, we need to do one more to
reviews endpoint. luckily, the first request included our API key and business ID, so we can reuse them in the 2nd request.
What's going on... Most of what we need is available from the 1st request so we can assign the name, icon, rating, total and url. What is missing are the reviews themselves. So we need to make a 2nd request to the
reviews endpoint. This endpoint will provide us the 5 most recent reviews from this business listing. This request will be assigned to
At this point in the API, we need to assign the individual review data to each area of the testimonial display. This includes the user's name, image, message, link to review, rating, date, and a unique ID. This the final step in assigning the data from the API platform.
What's going on... We're taking the data that was assigned in the previous step and assigning each part to the individual parts of the single review. Depending on the API source, not all fields can be used. In the case of Zomato, they provide data for each area we need.
API Branding Assets
Our last step is optional depending on the API guidelines from your source. Some require their branding, color scheme, or logo clearly shown on the testimonial. You will need to refer to their documentation for specifics. For Zomato, we need their "Powered by Zomato" image and main logo which will be used in the badge shortcode.
What's going on... We're going to setup the branding default rules for the source. This includes the branding for the regular testimonial list and the badge.
With all of the code usage shown above, below will be the output. The only difference is that we've assigned the two images for the badge logo and "Powered by Zomato" branding used in
bne_testimonials_api_branding_assets(). All code samples above are part of this gist.