This hook allows developers to modify the URL of the KML layer in WP Maps Pro. It is triggered when the KML layer URL is being set or retrieved, providing an opportunity to alter it at runtime.
Usage
Developers can implement this hook in their theme’s functions.php
file or within a custom plugin. It is particularly useful for dynamically changing the KML URL based on specific conditions or configurations. To ensure compatibility and prevent conflicts, always sanitize and validate any data before using it.
Example 1
This example demonstrates how to change the KML URL based on a specific condition.
add_filter('wpgmp_kml_layer', 'custom_kml_layer_url', 10, 2); function custom_kml_layer_url($kml_url, $map) { if ($map->ID == 1) { // Check if the map ID is 1 $kml_url = 'https://example.com/custom-layer.kml'; } return $kml_url; }
Example 2
Here, the KML URL is modified to add a timestamp as a query parameter, ensuring the latest version is always loaded.
add_filter('wpgmp_kml_layer', 'append_timestamp_to_kml_url', 10, 2); function append_timestamp_to_kml_url($kml_url, $map) { $kml_url = add_query_arg('v', time(), $kml_url); // Append current timestamp return $kml_url; }
Example 3
In this case, the KML URL is altered based on user roles, providing different content for different types of users.
add_filter('wpgmp_kml_layer', 'role_based_kml_layer_url', 10, 2); function role_based_kml_layer_url($kml_url, $map) { if (current_user_can('administrator')) { $kml_url = 'https://example.com/admin-layer.kml'; } elseif (current_user_can('subscriber')) { $kml_url = 'https://example.com/subscriber-layer.kml'; } return $kml_url; }
Remember to always validate and sanitize external URLs to prevent security vulnerabilities. Test your code in a development environment before deploying it to production.