The wpgmp_markers hook in the WP Maps Pro plugin allows developers to customize the final list of markers assigned to a map. It is triggered when the map is being generated, providing an opportunity to modify the list of markers before they are rendered on the map.
Usage
Developers can implement this hook by adding a custom function to their theme’s functions.php
file or a custom plugin. It is useful for altering marker data, such as filtering markers based on custom conditions or enhancing marker details dynamically.
Example 1
This example demonstrates how to filter markers based on a custom meta value.
add_filter('wpgmp_markers', 'filter_markers_by_meta', 10, 2); function filter_markers_by_meta($filterd_places, $map_id) { $filtered = array(); foreach ($filterd_places as $place) { if (get_post_meta($place->ID, 'custom_meta_key', true) == 'desired_value') { $filtered[] = $place; } } return $filtered; }
Example 2
This example shows how to add additional information to each marker.
add_filter('wpgmp_markers', 'add_info_to_markers', 10, 2); function add_info_to_markers($filterd_places, $map_id) { foreach ($filterd_places as &$place) { $place->extra_info = get_post_meta($place->ID, 'extra_info', true); } return $filterd_places; }
Example 3
This example illustrates how to change marker icons based on a category.
add_filter('wpgmp_markers', 'change_marker_icons_by_category', 10, 2); function change_marker_icons_by_category($filterd_places, $map_id) { foreach ($filterd_places as &$place) { $category = get_the_terms($place->ID, 'category'); if (!empty($category)) { $place->icon = get_template_directory_uri() . '/images/icons/' . $category[0]->slug . '.png'; } } return $filterd_places; }
Always ensure that data used in hooks is validated and sanitized to maintain security and performance. Additionally, test changes in a staging environment before deploying to a live site.