The wpgmp_sorting hook in the WP Maps Pro plugin allows developers to customize the sorting options for the listings displayed below the map. This hook is triggered whenever the sorting functionality needs to be rendered or modified, enabling developers to adjust sorting behavior at runtime.
Usage
Developers can implement this hook within their theme’s functions.php file or a custom plugin. It is useful for altering the default sorting options provided by WP Maps Pro, adding new sorting criteria, or modifying existing ones. This hook accepts two parameters: $sorting_array
, which is an array of current sorting options, and $map
, which contains map-specific information.
Example 1
This example demonstrates how to add a new sorting option based on a custom field called “Distance”.
add_filter('wpgmp_sorting', 'custom_sorting_options', 10, 2); function custom_sorting_options($sorting_array, $map) { $sorting_array['distance__asc'] = 'Nearest First'; $sorting_array['distance__desc'] = 'Farthest First'; return $sorting_array; }
Example 2
In this example, we modify the label of an existing sorting option for the address in ascending order.
add_filter('wpgmp_sorting', 'modify_sorting_label', 10, 2); function modify_sorting_label($sorting_array, $map) { if(isset($sorting_array['address__asc'])) { $sorting_array['address__asc'] = 'Address A-Z (Modified)'; } return $sorting_array; }
Example 3
This example illustrates how to remove a sorting option, specifically the “Z-A Title” option.
add_filter('wpgmp_sorting', 'remove_sorting_option', 10, 2); function remove_sorting_option($sorting_array, $map) { unset($sorting_array['title__desc']); return $sorting_array; }
Tip: Always ensure that your modifications do not conflict with other plugins or themes. Test changes in a staging environment before applying them to a live site to maintain the stability and security of your website.