public
Created

Using WordPress custom fields to drive WP Flexible Maps

  • Download Gist
gistfile1.phtml
HTML+PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
<?php
 
/*
In reply to this request:
http://wordpress.org/support/topic/plugin-wp-flexible-map-updating-the-map-from-custom-field-data
 
add a simple [map] shortcode to WordPress that allows maps to be
created from custom fields:
 
"map zoom" = the zoom level (will default to 12 if not set)
"map coordinates" = coordinates for the map
"map address" = address for the map
 
You must specify either coordinates or address (not both).
*/
 
if (!is_admin()) {
add_shortcode('map', 'custom_shortcodeMap');
}
 
/**
* handle shortcode for map display
*
* @param array shortcode attributes as supplied by the WP shortcode API
* @return string output to substitute for the shortcode
*/
function custom_shortcodeMap($attrs) {
global $post;
 
$html = '';
 
if (isset($post) && function_exists('flexmap_show_map')) {
$zoom = get_post_meta($post->ID, 'map zoom', true);
if (!$zoom) {
$zoom = 12;
}
 
$coords = get_post_meta($post->ID, 'map coordinates', true);
if ($coords) {
$map = array(
'center' => $coords,
'width' => 500,
'height' => 400,
'zoom' => $zoom,
);
$html = apply_filters('flexmap_getmap', $map);
}
else {
$address = get_post_meta($post->ID, 'map address', true);
if ($address) {
$map = array(
'address' => $address,
'width' => 500,
'height' => 400,
'zoom' => $zoom,
);
$html = apply_filters('flexmap_getmap', $map);
}
}
}
 
return $html;
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.