Skip to content

Instantly share code, notes, and snippets.

@webaware
Created June 26, 2012 22:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save webaware/2999934 to your computer and use it in GitHub Desktop.
Save webaware/2999934 to your computer and use it in GitHub Desktop.
Using WordPress custom fields to drive WP Flexible Maps
<?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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment