Fetch data from outside & display them in a widget
-
hello dear experts good day
i really like your ACF-plugin and all the awesome features i see.
btw: i want to perform a retrieval to the endpoint of OSM – eg like so. – this is the idea to work on the Open-Streetmap-API – Endpoint:
see the request[out:csv(::id,::type,::lon,::lat,amenity,name,"addr:postcode","addr:city","addr:street","addr:housenumber","contact:website",website,"contact:email")]
[timeout:600];
rel[boundary=administrative][admin_level=6][name="München"] -> .city;
(nwr[amenity=hospital][name](around.city:200);
nwr[amenity=school][name](around.city:200);
nwr[amenity=church][name](around.city:200);
nwr[amenity=childcare][name](around.city:200);
nwr[amenity=nursing_home][name](around.city:200););
out center;with the following approach
<?php
/*
Plugin Name: ACF Schools Widget
Description: Displays nearby schools using Overpass Turbo and ACF.
Version: 1.0
Author: foo bar
*/
// Exit if accessed directly
if (!defined('ABSPATH')) exit;
// Include ACF
include_once(plugin_dir_path(__FILE__) . 'acf/acf.php');
// Register ACF fields
function register_acf_fields() {
if (function_exists('acf_add_local_field_group')) {
acf_add_local_field_group(array(
'key' => 'group_1',
'title' => 'Nearby Schools Widget',
'fields' => array(
array(
'key' => 'field_1',
'label' => 'Latitude',
'name' => 'latitude',
'type' => 'number',
'required' => 1,
),
array(
'key' => 'field_2',
'label' => 'Longitude',
'name' => 'longitude',
'type' => 'number',
'required' => 1,
),
array(
'key' => 'field_3',
'label' => 'Radius (km)',
'name' => 'radius',
'type' => 'number',
'required' => 1,
),
),
'location' => array(
array(
array(
'param' => 'post_type',
'operator' => '==',
'value' => 'page',
),
),
),
));
}
}
add_action('acf/init', 'register_acf_fields');
// Enqueue necessary scripts
function acf_schools_widget_enqueue_scripts() {
wp_enqueue_script('jquery');
}
add_action('wp_enqueue_scripts', 'acf_schools_widget_enqueue_scripts');
// Shortcode to display schools
function display_nearby_schools($atts) {
$latitude = get_field('latitude');
$longitude = get_field('longitude');
$radius = get_field('radius');
if (!$latitude || !$longitude || !$radius) {
return 'Please provide latitude, longitude, and radius.';
}
// Overpass API URL
$query = '[out:json][timeout:25];(node["amenity"="school"](around:' . ($radius * 1000) . ',' . $latitude . ',' . $longitude . '););out body;>;out skel qt;';
$url = 'https://overpass-api.de/api/interpreter?data=' . urlencode($query);
// Fetch data from Overpass API
$response = wp_remote_get($url);
if (is_wp_error($response)) {
return 'Unable to retrieve data.';
}
$body = wp_remote_retrieve_body($response);
$data = json_decode($body, true);
if (empty($data['elements'])) {
return 'No schools found in the specified area.';
}
// Display data
$output = '<ul class="nearby-schools">';
foreach ($data['elements'] as $element) {
if (isset($element['tags']['name'])) {
$output .= '<li>';
$output .= esc_html($element['tags']['name']);
if (isset($element['tags']['website'])) {
$output .= ' - <a href="' . esc_url($element['tags']['website']) . '" target="_blank">' . esc_html($element['tags']['website']) . '</a>';
}
$output .= '</li>';
}
}
$output .= '</ul>';
return $output;
}
add_shortcode('nearby_schools', 'display_nearby_schools');and subsequently i want to display the output in a widget
@id @type @lon @lat amenity name addr:postcode addr:city addr:street addr:housenumber contact:website website
@id @type @lon @lat amenity name addr:postcode addr:city addr:street addr:housenumber contact:website website contact:email
703266518 node 11.5746643 48.1387135 school EAM School of International Business 80331 München Frauenplatz 11 https://eam-muenchen.com/
1187338076 node 11.6800258 48.0835510 childcare Little Giants 81739 München Asenweg 18 https://www.littlegiants.de/kitas/kita/muenchen-waldperlach/
1187338193 node 11.6779495 48.0840912 childcare Kinderhaus Waldperlach 81739 München Asenweg 2 https://www.kinderhaus-muenchen.de
1719550854 node 11.5115441 48.0586595 school Erzbisch?fliche Pater-Rupert-Mayer-Volksschule Pullach https://www.prmvs.de/index.php?id=19 https://www.prmvs.de/index.php?id=19 [email protected]
1719550857 node 11.5128669 48.0605418 school Pater-Rupert-Mayer-Realschule https://www.prmrs.de/index.php?id=26or at least a part of this
well i guess that i can do so – with the ACF-Plugin
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
- You must be logged in to reply to this topic.