Set a settings page field from external api
Support › MB Settings Page › Set a settings page field from external api
- This topic has 1 reply, 2 voices, and was last updated 1 year, 1 month ago by
Peter.
-
AuthorPosts
-
September 25, 2024 at 6:13 AM #46531
[email protected]
ParticipantI 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");September 26, 2024 at 8:42 PM #46540Peter
ModeratorHello,
Do you use ACF to update the field value with the function
update_field()? If you use Meta Box and settings page, please use the functionrwmb_set_meta()to update the field value on the settings page. Please follow the documentation
https://docs.metabox.io/functions/rwmb-set-meta/ -
AuthorPosts
- You must be logged in to reply to this topic.