map Archives - Page 2 of 3 - WP Maps Pro

15000+ live websites are using this google maps wordpress plugin.

Hook Category: map

This hook in the WP Maps Pro plugin allows developers to customize the display of the transit layer on a map at runtime. It is triggered when the map is being rendered, giving the opportunity to show or hide the transit layer based on specific conditions.

Usage

Developers can implement this hook in their theme’s functions.php file or a custom plugin. It is useful for dynamically controlling the visibility of the transit layer, depending on various factors such as user roles or custom settings.

Example 1

This example hides the transit layer for non-admin users.

add_filter('wpgmp_transit_layer', function($display_layer, $map) {
    if (!current_user_can('administrator')) {
        $display_layer = false;
    }
    return $display_layer;
}, 10, 2);

Example 2

In this example, the transit layer is shown only during weekdays.

add_filter('wpgmp_transit_layer', function($display_layer, $map) {
    $current_day = date('N'); // 1 (for Monday) through 7 (for Sunday)
    if ($current_day >= 1 && $current_day <= 5) {
        $display_layer = true;
    } else {
        $display_layer = false;
    }
    return $display_layer;
}, 10, 2);

Example 3

This example modifies the transit layer visibility based on a map-specific custom setting.

add_filter('wpgmp_transit_layer', function($display_layer, $map) {
    $custom_setting = get_post_meta($map->ID, 'transit_layer_setting', true);
    if ($custom_setting === 'show') {
        $display_layer = true;
    } else {
        $display_layer = false;
    }
    return $display_layer;
}, 10, 2);

Remember to always validate and sanitize any data used within hooks to ensure security and stability of your WordPress site.

This hook in the WP Maps Pro plugin allows developers to customize the URL of map tiles for Leaflet-based maps. It is triggered when the map tiles are being set up, allowing for the modification of the default map tile URL.

Usage

Developers can implement this hook by adding a filter in their theme’s functions.php file or a custom plugin. It is particularly useful for changing the map tile source to a different provider or implementing self-hosted tiles for improved performance or styling.

Example 1

Changing the map tile URL to use a different tile provider, such as Mapbox.

add_filter( 'wpgmp_map_tile_url', function( $tile_url, $map ) {
    return 'https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token={accessToken}';
}, 10, 2 );

Example 2

Using self-hosted map tiles for a custom look and feel.

add_filter( 'wpgmp_map_tile_url', function( $tile_url, $map ) {
    return 'https://mywebsite.com/tiles/{z}/{x}/{y}.png';
}, 10, 2 );

Example 3

Switching to a different OpenStreetMap server to reduce load on the default server.

add_filter( 'wpgmp_map_tile_url', function( $tile_url, $map ) {
    return 'https://{s}.tile.openstreetmap.de/{z}/{x}/{y}.png';
}, 10, 2 );

Tip: When modifying map tile URLs, always ensure you comply with the service provider’s terms of use and handle any required API keys securely. Avoid exposing sensitive information in your code.

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.

This hook allows developers to customize the display of the bicycle layer on a map within the WP Maps Pro plugin. It is triggered when the map is being rendered and determines whether the bicycle layer should be shown or hidden. The modifiable data is a boolean value within an array, specifically {“display_layer”:true}.

Usage

Developers can implement this hook in their theme’s functions.php file or a custom plugin. It provides a way to conditionally show or hide the bicycle layer based on certain conditions or settings. By attaching a custom function to this hook, developers can manipulate the visibility of the bicycle layer dynamically.

Example 1

This example demonstrates how to hide the bicycle layer for a specific map.

add_filter('wpgmp_bicycling_layer', 'custom_hide_bicycle_layer', 10, 2);
function custom_hide_bicycle_layer($map_data, $map) {
    if ($map->ID == 123) { // Assuming 123 is the map ID
        $map_data['display_layer'] = false;
    }
    return $map_data;
}

Example 2

Show the bicycle layer only if the current user is logged in.

add_filter('wpgmp_bicycling_layer', 'show_bicycle_layer_for_logged_in_users', 10, 2);
function show_bicycle_layer_for_logged_in_users($map_data, $map) {
    if (is_user_logged_in()) {
        $map_data['display_layer'] = true;
    } else {
        $map_data['display_layer'] = false;
    }
    return $map_data;
}

Example 3

Toggle the bicycle layer based on a custom field value associated with the map.

add_filter('wpgmp_bicycling_layer', 'toggle_bicycle_layer_based_on_custom_field', 10, 2);
function toggle_bicycle_layer_based_on_custom_field($map_data, $map) {
    $custom_field_value = get_post_meta($map->ID, 'show_bicycle_layer', true);
    if ($custom_field_value === 'yes') {
        $map_data['display_layer'] = true;
    } else {
        $map_data['display_layer'] = false;
    }
    return $map_data;
}

Always validate and sanitize any data used within your hook functions to ensure secure and reliable code.

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.

This hook allows developers to customize the map type of OpenStreetMap in the WP Maps Pro plugin. It is triggered when the map is being rendered, enabling changes to the tile sources for OpenStreetMap.

Usage

Developers can implement this hook in their theme’s functions.php file or a custom plugin to alter the available map styles. This can be useful for adding new map styles or modifying existing ones to suit specific needs.

Example 1

Add a custom map style to the list of OpenStreetMap styles.

add_filter('wpgmp_openstreet_style', 'add_custom_map_style');
function add_custom_map_style($openstreet_styles) {
    $openstreet_styles['CustomMap'] = 'https://custom.tile.server/{z}/{x}/{y}.png';
    return $openstreet_styles;
}

Example 2

Modify the URL of an existing map style to point to a different tile server.

add_filter('wpgmp_openstreet_style', 'modify_map_style_url');
function modify_map_style_url($openstreet_styles) {
    if (isset($openstreet_styles['OpenStreetMap.Mapnik'])) {
        $openstreet_styles['OpenStreetMap.Mapnik'] = 'https://another.tile.server/{z}/{x}/{y}.png';
    }
    return $openstreet_styles;
}

Example 3

Remove a specific map style from the available options.

add_filter('wpgmp_openstreet_style', 'remove_map_style');
function remove_map_style($openstreet_styles) {
    unset($openstreet_styles['OpenStreetMap.BZH']);
    return $openstreet_styles;
}

Tip: Always validate and sanitize any external URLs added via this hook to ensure they are secure and trustworthy, preventing potential security vulnerabilities.

This hook in the WP Maps Pro plugin allows developers to insert custom HTML before the map element. It is triggered before the map is rendered on the frontend, providing an opportunity to customize the content or layout directly above the map.

Usage

Developers can implement this hook in their theme’s functions.php file or within a custom plugin. It is essential to ensure that the code is efficient and does not interfere with the map’s functionality. Consider using this hook to add additional information, instructions, or custom controls related to the map.

Example 1

Add a custom title above the map to provide context or instructions to users.

add_action('wpgmp_before_map', function($map) {
    echo '<h3>Explore Our Locations</h3>';
}, 10, 1);

Example 2

Insert a custom advertisement banner above the map to promote related services or offers.

add_action('wpgmp_before_map', function($map) {
    echo '<div class="map-ad-banner">Check out our latest travel guides!</div>';
}, 10, 1);

Example 3

Display user-specific information by accessing user data before the map is displayed.

add_action('wpgmp_before_map', function($map) {
    if(is_user_logged_in()) {
        $current_user = wp_get_current_user();
        echo '<p>Welcome back, ' . esc_html($current_user->display_name) . '!</p>';
    }
}, 10, 1);

Tip: When using this hook, ensure that any HTML output is properly sanitized and escaped to prevent security vulnerabilities, such as XSS attacks.

This hook in the WP Maps Pro plugin allows developers to customize the output of the map, listing, and filter HTML sections. It is triggered during the runtime, specifically when the map output is being generated, enabling changes to the layout or styling of these sections.

Usage

Developers can implement this hook in their theme’s functions.php file or within a custom plugin. By using this hook, you can manipulate the HTML output of the map, filter, and listing sections, allowing for dynamic customization based on specific requirements.

Example 1

This example demonstrates how to prepend a custom message above the map output.

add_filter('wpgmp_map_output', 'custom_map_message', 10, 5);
function custom_map_message($output, $map_div, $filters_div, $listing_div, $map_id) {
    $custom_message = '<div class="custom-message">Welcome to our custom map!</div>';
    return $custom_message . $output;
}

Example 2

In this example, we modify the map’s HTML to include a custom CSS class for additional styling.

add_filter('wpgmp_map_output', 'add_custom_class_to_map', 10, 5);
function add_custom_class_to_map($output, $map_div, $filters_div, $listing_div, $map_id) {
    $map_div = str_replace('class="wpgmp_map"', 'class="wpgmp_map custom-map-class"', $map_div);
    return $map_div . $filters_div . $listing_div;
}

Example 3

This example illustrates how to conditionally remove the filter section based on the map ID.

add_filter('wpgmp_map_output', 'conditionally_remove_filters', 10, 5);
function conditionally_remove_filters($output, $map_div, $filters_div, $listing_div, $map_id) {
    if ($map_id == 1) {
        // Remove filters for map ID 1
        $filters_div = '';
    }
    return $map_div . $filters_div . $listing_div;
}

Tip: Always ensure you sanitize and validate any data before outputting it to prevent security vulnerabilities such as XSS attacks.

This hook allows developers to modify the map data before any processing occurs in the WP Maps Pro plugin. It is triggered before the map data is processed, enabling customization of map settings and features at runtime.

Usage

Developers can implement this hook in their theme’s functions.php file or a custom plugin to adjust the map data dynamically. This is especially useful for altering map settings based on specific conditions, user roles, or other runtime variables.

Example 1

Modify the map’s zoom level based on the current user’s role.

add_filter('wpgmp_map_data', 'customize_map_zoom_level', 10, 2);

function customize_map_zoom_level($map_data, $map) {
    if (current_user_can('administrator')) {
        $map_data['map_zoom_level'] = '10'; // Set higher zoom for administrators
    }
    return $map_data;
}

Example 2

Change the map type to ‘SATELLITE’ for a specific map ID.

add_filter('wpgmp_map_data', 'set_satellite_map_type', 10, 2);

function set_satellite_map_type($map_data, $map) {
    if ($map_data['map_id'] == '10') {
        $map_data['map_type'] = 'SATELLITE';
    }
    return $map_data;
}

Example 3

Add custom CSS to the map’s settings dynamically.

add_filter('wpgmp_map_data', 'add_custom_map_css', 10, 2);

function add_custom_map_css($map_data, $map) {
    $map_data['wpgmp_custom_css'] .= '.custom-marker { background-color: #ff0000; }';
    return $map_data;
}

Tip: Always validate and sanitize any input data when using hooks to ensure security and prevent vulnerabilities in your WordPress site.

The wpgmp_map_class hook in WP Maps Pro allows developers to customize the CSS class of the map element div at runtime. This hook is useful for modifying the appearance or behavior of map elements by adding custom CSS classes.

Usage

Developers can implement this hook in their theme’s functions.php file or within a custom plugin. The hook is used to append or modify CSS classes on map div elements, enabling tailored styling or additional functionality.

Example 1

Description of the first use-case: Adding a unique CSS class to a map element for custom styling.

add_filter( 'wpgmp_map_class', 'add_custom_map_class', 10, 2 );

function add_custom_map_class( $classes, $map ) {
    $classes .= ' my-custom-class';
    return $classes;
}

Example 2

Description of the second use-case: Conditionally adding a CSS class based on map ID.

add_filter( 'wpgmp_map_class', 'conditional_map_class', 10, 2 );

function conditional_map_class( $classes, $map ) {
    if ( $map->ID == 5 ) {
        $classes .= ' special-map-class';
    }
    return $classes;
}

Example 3

Description of the third use-case: Removing an existing class and adding a new one.

add_filter( 'wpgmp_map_class', 'replace_map_class', 10, 2 );

function replace_map_class( $classes, $map ) {
    $classes = str_replace( 'old-class', '', $classes );
    $classes .= ' new-class';
    return $classes;
}

Tip: Always validate and sanitize inputs when modifying hooks to ensure security and prevent potential conflicts or errors in your WordPress site.

Install Plugin Now!

WP MAPS PRO helps you create dynamic, customizable maps using Google Maps or Leaflet — no coding required. Includes free updates, premium support, and a 30-day money-back guarantee.