Set a settings page field from external api

Support MB Settings Page Set a settings page field from external api

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #46531
    admin@dantianhealth.com.au[email protected]
    Participant

    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");
    #46540
    PeterPeter
    Moderator

    Hello,

    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 function rwmb_set_meta() to update the field value on the settings page. Please follow the documentation
    https://docs.metabox.io/functions/rwmb-set-meta/

Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.