Skip to content

Instantly share code, notes, and snippets.

@stavros-melidoniotis
Last active January 12, 2023 07:32
Show Gist options
  • Save stavros-melidoniotis/d8e8347b7b7a873f0d5d9b7ba8d1a1f2 to your computer and use it in GitHub Desktop.
Save stavros-melidoniotis/d8e8347b7b7a873f0d5d9b7ba8d1a1f2 to your computer and use it in GitHub Desktop.
Leaflet basic config for Wordpress
<?php
function enqueue_styles_and_scripts()
{
wp_enqueue_script('leafletjs', get_template_directory_uri() . '/path/to/leaflet-example.js', array(), '1.0.0', true);
}
add_action('wp_enqueue_scripts', 'enqueue_styles_and_scripts');
<head>
// content before
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet.js"></script>
// or if you've downloaded the leafletjs files
<link rel="stylesheet" href="<?= get_template_directory_uri() . '/path/to/leaflet.css' ?>" />
<script src="<?= get_template_directory_uri() . '/path/to/leaflet.js' ?>"></script>
// content after
</head>
// create the map element inside the div with id=map
const map = L.map('map', {
center: [<map_lat>, <map_lng>],
zoom: <map_zoom>,
})
// add the openstreetmap tileLayer
L.tileLayer(`https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png`, {
attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors',
}).addTo(map)
// OPTIONAL: create a custom marker icon
const markerIcon = L.icon({
iconUrl: 'path/to/custom_icon',
iconSize: [40, 40],
})
// OPTIONAL: add the marker with custom icon to the map
L.marker([<marker_lat>, <marker_lng>], { icon: markerIcon }).addTo(map);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment