This hook in the WP Maps Pro plugin allows developers to customize the visibility of the traffic layer on a map. It is triggered when the map is being rendered, giving developers the ability to show or hide the traffic layer dynamically.
Usage
Developers can implement this hook within their theme’s functions.php file or a custom plugin. It allows for the modification of the traffic layer display at runtime by using the modifiable data provided. Practical tips include checking user roles or other conditions to determine whether the traffic layer should be shown.
Example 1
Hide the traffic layer for non-admin users.
add_filter('wpgmp_traffic_layer', 'customize_traffic_layer_visibility', 10, 2); function customize_traffic_layer_visibility($traffic_layer, $map) { if(!current_user_can('administrator')) { $traffic_layer['display_layer'] = false; } return $traffic_layer; }
Example 2
Show the traffic layer only on specific maps based on the map ID.
add_filter('wpgmp_traffic_layer', 'show_traffic_layer_on_specific_map', 10, 2); function show_traffic_layer_on_specific_map($traffic_layer, $map) { if($map['id'] == 5) { $traffic_layer['display_layer'] = true; } else { $traffic_layer['display_layer'] = false; } return $traffic_layer; }
Example 3
Toggle traffic layer visibility based on a custom user meta setting.
add_filter('wpgmp_traffic_layer', 'toggle_traffic_layer_based_on_user_meta', 10, 2); function toggle_traffic_layer_based_on_user_meta($traffic_layer, $map) { $user_id = get_current_user_id(); $show_traffic = get_user_meta($user_id, 'show_traffic_layer', true); $traffic_layer['display_layer'] = ($show_traffic === 'yes'); return $traffic_layer; }
Tip: Always ensure that the logic within the filter is optimized for performance, especially if it involves database queries or user checks, to prevent slowing down the map rendering process.