Trying to integrate Google API abilities into my WordPress site using ACF [closed]

Closed. This question needs debugging details. It is not currently accepting answers. Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question. Closed 8 days ago. Improve this question I have been trying to learn and figure out how to collect and display data from Google APIs like elevation, coordinates and air quality to be displayed in a read only custom field, but I am having a difficult time figuring out why things aren't working. Using ACF Pro and ACF Extended alongside Elementor Pro. This is the code I have in my functions.php. I can't figure out if there's a step I'm missing or if this code is junk. Can anyone help me understand why it's not working and possibly help me learn to write this correctly? /** This code is to acquire Google Maps Elevation */ function get_elevation_data($latitude, $longitude) { $api_key = 'API-KEY'; $url = "https://maps.googleapis.com/maps/api/elevation/json?locations={$latitude},{$longitude}&key={$api_key}"; $response = wp_remote_get($url); if (is_wp_error($response)) { return false; } $body = wp_remote_retrieve_body($response); $data = json_decode($body); if (isset($data->results[0]->elevation)) { return $data->results[0]->elevation; } return false; } function save_elevation_data($post_id) { if (get_post_type($post_id) !== 'parking') { return; } $latitude = get_field('latitude', $post_id); $longitude = get_field('longitude', $post_id); if ($latitude && $longitude) { $elevation = get_elevation_data($latitude, $longitude); if ($elevation !== false) { update_field('elevation', $elevation, $post_id); } } } add_action('save_post', 'save_elevation_data'); function make_elevation_field_readonly($field) { if ($field['name'] == 'elevation') { $field['readonly'] = true; } return $field; } add_filter('acf/prepare_field', 'make_elevation_field_readonly'); /** Fetch Geolocation Data */ function get_geolocation_data($address) { $api_key = 'API-KEY'; $address = urlencode($address); $url = "https://maps.googleapis.com/maps/api/geocode/json?address={$address}&key={$api_key}"; $response = wp_remote_get($url); if (is_wp_error($response)) { return false; } $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if ($data['status'] === 'OK') { $location = $data['results'][0]['geometry']['location']; return $location; } return false; } /** Fetch Air Quality Data */ function get_air_quality_data($lat, $lng) { $api_key = 'API-KEY'; $url = "https://maps.googleapis.com/maps/api/air_quality/v1/current?lat={$lat}&lng={$lng}&key={$api_key}"; $response = wp_remote_get($url); if (is_wp_error($response)) { return false; } $body = wp_remote_retrieve_body($response); $data = json_decode($body, true); if ($data['status'] === 'OK') { return $data['data']; } return false; } /** Add Maps Air Quality API info */ function display_air_quality($atts) { $atts = shortcode_atts(array('post_id' => ''), $atts, 'air_quality'); $post_id = $atts['post_id']; $address = get_field('location', $post_id); if (!$address || !isset($address['address'])) { return 'Address not found.'; } $location = get_geolocation_data($address['address']); if (!$location) { return 'Geolocation data not found.'; } $air_quality = get_air_quality_data($location['lat'], $location['lng']); if (!$air_quality) { return 'Air quality data not found.'; } ob_start(); ?> <div class="air-quality"> <p>Air Quality Index: <?php echo esc_html($air_quality['aqi']); ?></p> <p>Main Pollutant: <?php echo esc_html($air_quality['main_pollutant']); ?></p> </div> <?php return ob_get_clean(); } add_shortcode('air_quality', 'display_air_quality');

Comment (0)

You’ll be in good company