Guest User

Untitled

a guest
Jun 24th, 2025
22
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.80 KB | None | 0 0
  1. function custom_map_shortcode( $atts ) {
  2. $atts = shortcode_atts(
  3. array(
  4. 'id' => 'maps_location',
  5. 'object_id' => get_queried_object_id(),
  6. ),
  7. $atts
  8. );
  9.  
  10. if ( ! $atts['id'] || ! $atts['object_id'] ) {
  11. return 'Field ID and post context are required.';
  12. }
  13.  
  14. $main_location = rwmb_meta( $atts['id'], '', $atts['object_id'] );
  15. if ( empty( $main_location ) ) {
  16. return 'No primary map data found.';
  17. }
  18.  
  19. $extra_pins = array(
  20. array(
  21. 'latitude' => 53.46565740326415,
  22. 'longitude' => -2.2326974824510635,
  23. 'title' => 'University of Manchester',
  24. 'content' => '<strong>Manchester</strong><br>Premier university.',
  25. 'icon' => '/wp-content/uploads/2025/03/Map-Pin-Small-2.svg',
  26. ),
  27. array(
  28. 'latitude' => -33.920,
  29. 'longitude' => 18.423,
  30. 'title' => 'Cape Peninsula University of Technology',
  31. 'content' => 'CPUT Campus',
  32. 'icon' => '',
  33. ),
  34. );
  35.  
  36. $map_args = array(
  37. 'width' => '100%',
  38. 'height' => '480px',
  39. 'zoom' => 13,
  40. 'marker' => true,
  41. 'marker_icon' => '/wp-content/uploads/2025/03/Map-Pin-Small-2.svg',
  42. 'js_options' => array(
  43. 'mapTypeId' => 'HYBRID',
  44. 'zoomControl' => true,
  45. 'zoomControlPosition' => 'topright',
  46. 'markers' => array(), // Initialise empty markers array
  47. ),
  48. );
  49.  
  50. // Add extra pins
  51. foreach ( $extra_pins as $pin ) {
  52. $map_args['js_options']['markers'][] = array(
  53. 'latitude' => $pin['latitude'],
  54. 'longitude' => $pin['longitude'],
  55. 'title' => $pin['title'],
  56. 'infoWindow' => $pin['content'],
  57. 'markerIcon' => $pin['icon'],
  58. );
  59. }
  60.  
  61. ob_start();
  62. rwmb_the_value( $atts['id'], $map_args, $atts['object_id'] );
  63. return ob_get_clean();
  64. }
  65. add_shortcode( 'custom_map', 'custom_map_shortcode' );
Advertisement
Add Comment
Please, Sign In to add comment