Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function custom_map_shortcode( $atts ) {
- $atts = shortcode_atts(
- array(
- 'id' => 'maps_location',
- 'object_id' => get_queried_object_id(),
- ),
- $atts
- );
- if ( ! $atts['id'] || ! $atts['object_id'] ) {
- return 'Field ID and post context are required.';
- }
- $main_location = rwmb_meta( $atts['id'], '', $atts['object_id'] );
- if ( empty( $main_location ) ) {
- return 'No primary map data found.';
- }
- $extra_pins = array(
- array(
- 'latitude' => 53.46565740326415,
- 'longitude' => -2.2326974824510635,
- 'title' => 'University of Manchester',
- 'content' => '<strong>Manchester</strong><br>Premier university.',
- 'icon' => '/wp-content/uploads/2025/03/Map-Pin-Small-2.svg',
- ),
- array(
- 'latitude' => -33.920,
- 'longitude' => 18.423,
- 'title' => 'Cape Peninsula University of Technology',
- 'content' => 'CPUT Campus',
- 'icon' => '',
- ),
- );
- $map_args = array(
- 'width' => '100%',
- 'height' => '480px',
- 'zoom' => 13,
- 'marker' => true,
- 'marker_icon' => '/wp-content/uploads/2025/03/Map-Pin-Small-2.svg',
- 'js_options' => array(
- 'mapTypeId' => 'HYBRID',
- 'zoomControl' => true,
- 'zoomControlPosition' => 'topright',
- 'markers' => array(), // Initialise empty markers array
- ),
- );
- // Add extra pins
- foreach ( $extra_pins as $pin ) {
- $map_args['js_options']['markers'][] = array(
- 'latitude' => $pin['latitude'],
- 'longitude' => $pin['longitude'],
- 'title' => $pin['title'],
- 'infoWindow' => $pin['content'],
- 'markerIcon' => $pin['icon'],
- );
- }
- ob_start();
- rwmb_the_value( $atts['id'], $map_args, $atts['object_id'] );
- return ob_get_clean();
- }
- add_shortcode( 'custom_map', 'custom_map_shortcode' );
Advertisement
Add Comment
Please, Sign In to add comment