Introduction:
This blog post explains the process of integrating the Google Places API into a WordPress website to display business reviews dynamically. The setup was not straightforward and involved multiple challenges, including API restrictions, debugging issues, and formatting the shortcode correctly. This post will cover the key steps taken, from setting up Google Places API, creating a custom WordPress plugin, troubleshooting API errors, and implementing an autocomplete search with review fetching.
Section 1: Setting Up Google Places API
Step 1: Create a Google Cloud Project
- Visit Google Cloud Console.
- Create a new project or select an existing one.
Step 2: Enable the Required APIs
- Go to APIs & Services > Library.
- Enable the following APIs:
- Google Places API
- Maps JavaScript API
Step 3: Generate API Keys
- Go to APIs & Services > Credentials.
- Click “Create Credentials” > “API Key”.
- Create two API keys:
- One for frontend JavaScript requests, restricted to HTTP referrers.
- One for backend PHP requests, restricted to the server’s IP address.
Section 2: Implementing the API on WordPress
Instead of using a third-party plugin, a custom WordPress plugin was created to handle API requests securely. The plugin allows users to search for businesses using autocomplete and display real-time reviews dynamically.
Step 1: Creating the WordPress Plugin
The plugin includes the following features:
- An autocomplete search box where users can type and select a business.
- A shortcode ( ) to display reviews on any page.
- AJAX-based API calls to fetch reviews without requiring a page reload.
Step 2: Writing the PHP Code for API Requests
Key functions in the plugin:
- fetch_place_id: retrieves the Google Place ID for a given business name.
- fetch_google_reviews: fetches customer reviews from the Google API.
- get_google_reviews_ajax: allows reviews to be dynamically retrieved without refreshing the page.
Section 3: Troubleshooting Issues and Fixing Errors
Issue 1: API keys with referer restrictions cannot be used with this API
Problem: The API key was restricted to HTTP referrers but Google Places API requires a server-side key.
Solution:
Created a second API key restricted to the server’s IP. Updated the plugin to use the backend API key for PHP requests.
Issue 2: No reviews found or invalid Place ID
Problem: Even with a valid Place ID, reviews were not being returned.
Solution:
Updated the API request to include name, reviews, user ratings total, and formatted address. Checked debug logs in wp-content/debug.log to inspect the API response.
Issue 3: Request Denied (API Key Restrictions)
Problem: The API key was blocked due to excessive restrictions.
Solution:
Adjusted API permissions in Google Cloud Console. Ensured the backend API key had access to Google Places API.
Section 4: Final Implementation and Results
After debugging and resolving API issues, the plugin successfully fetches and displays Google Places reviews. The final implementation allows users to enter a business name, select it using autocomplete, and retrieve customer reviews dynamically.
Conclusion: Lessons Learned
- Use separate API keys for frontend JavaScript and backend PHP requests.
- Enable detailed logging in wp-content/debug.log to troubleshoot API issues.
- Adjust API restrictions in Google Cloud Console to match the intended use case.
This guide can help others avoid common pitfalls and successfully integrate Google Places API into WordPress.
Call to Action
Have you integrated Google Places API before? What challenges did you face? Share your experience in the comments.