I have a settings page for business details (business-data) with a bunch of custom fields (business-settings). I would like to populate specific fields (rating, review) with data generated externally from an API (Google Places API) that is linked to the (place-id).
I have the following code that works to echo it onto a page using short code but am a bit lost as to how to output it to the location described previously. Could you please help point me in the correct direction? Thanks
function display_google_places_results()
{
// Replace with your API key
$api_key = "APIKEY";
// Replace with the place ID or name and location
$place_id = "PLACEID";
// Construct the API request URL
$url =
"https://maps.googleapis.com/maps/api/place/details/json?placeid=" .
$place_id .
"&key=" .
$api_key;
// Make the API request
$response = wp_remote_get($url);
// Check if the request was successful
if (!is_wp_error($response)) {
$body = wp_remote_retrieve_body($response);
$data = json_decode($body);
// Extract the star rating and number of reviews
$star_rating = $data->result->rating;
$num_reviews = $data->result->user_ratings_total;
$business_name = $data->result->name;
$google_profile_url = $data->result->url;
// Display the results
echo '<p><a href="' .
$google_profile_url .
'">' .
$business_name .
"</a></p>";
echo "<p>Star Rating: " . $star_rating . "</p>";
echo "<p>Number of Reviews: " . $num_reviews . "</p>";
update_field(
"reviews",
$value . " (" . $num_reviews . " Reviews)",
get_option("business-settings")
);
} else {
// Handle errors
echo "Error fetching Google Places data.";
}
}
add_shortcode("google_places_results", "display_google_places_results");