want to add a leaflet to a wordpress-widget
-
dear @gsarig dear experts, good day 😉
i want to add a leaflet to a wordpress-widget – see the thread here – which describes what is wanted.
sensu the options shown here: https://leafletjs.com/examples/quick-start/
i want ot add a leaflet that is created due to a request on overpass-turbo.eu –with the gathered data i want to add the data to a wiget in wordpress – is this possible!?
well it would be awesome if i can add this data set in a leaflet on widget:
dear @gsarig i look forward to hear form you
-
Hello and sorry for the delayed response. I’m not sure that I understand the request. Are you referring to the new Block-based Widges Editor, or the Classic Widgets?
good day dear friend,
well – what i like to do is the following:
i d like to add a search / or lets say a retrival – on the osm-endpoint that looks like so
[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:300);
nwr[amenity=school][name](around.city:300);
nwr[amenity=church][name](around.city:300);
nwr[amenity=childcare][name](around.city:300);
nwr[amenity=nursing_home][name](around.city:300););
out center;well that said : if i am able to fetch the data – then i d like to show this in a tiny table in a widget on my wordPress-site
<?php
/*
Plugin Name: the Schools Widget
Description: Displays nearby schools using Overpass Turbo and the awesome block.
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 = 'http://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');well – i want to fetch data form the openstreet-map and display it in a widget on the wordpress-site. What do you think – can we do so with the support of your plugin?
I don’t think that this plugin can help much as what you describe sounds like something that requires a custom implementation. You might be able to utilize the Custom field support, if you want to have a map on specific posts as a custom field instead of a block, but you would still have to implement the logic to fetch the data and manipulate according to your needs in order to display the locations in a different way.
good evening dear Giorgos
first of all : many many thanks for the quick reply – great to hear from you. Awesome. I have had a closer look at the option you recommended – this sounds very interesting – i will digg deeper into all that
I don’t think that this plugin can help much as what you describe sounds like something that requires a custom implementation. You might be able to utilize the Custom field support, if you want to have a map on specific posts as a custom field instead of a block, but you would still have to implement the logic to fetch the data and manipulate according to your needs in order to display the locations in a different way.
Awesome – i am happy to be part of this great forum – i am happy with your recommendation
all of that looks very promisinghttps://github.com/gsarig/ootb-openstreetmap/releases/tag/2.8.0
Custom field support: The highlight of version 2.8.0 is the support of a “Location” custom field, which allows you to store a post’s or a post type’s location. The data are stored following the official guidelines.
many thanks for all you do!!
plz keep up your great project – it rocks!!!Thanks for your kind words.
btw, if the custom field works for what you intend to do, then perhaps you can take a look at the [ootb_query] shortcode. For example, something like the below would output the locations of all posts with locations:
[ootb_query source="geodata"]If you would want to put it anywhere in your PHP files instead of the post editor, you could use do_shortcode, like:
<?php echo do_shortcode('[ootb_query source="geodata"]'); ?>For all the available options, you can check the plugin’s readme.
-
This reply was modified 1 year, 8 months ago by
Giorgos Sarigiannidis.
-
This reply was modified 1 year, 8 months ago by
The topic ‘want to add a leaflet to a wordpress-widget’ is closed to new replies.