Ok I figure how to allow Pronamic Google Maps metabox to be functional in a front end form. WP nonce security check is working for me since users have to log in with a contributor account to access the form.
Here is the code I used if for some reason somebody wants to do the same thing (using WP User Frontend function).
//////// PRONAMIC GOOGLE MAPS IN FRONT END //////////
/**
* Add google maps options to the add custom post area<br />
*
* @uses wpuf_add_post_form_description action hook
*
* @param string $post_type the post type of the post add screen
* @param object|null $post the post object
*/
function wpufe_gmaps( $post_type, $post = null) {
// Pronamic custom function to get custom fields
//$pgm = ( $post != null ) ? pronamic_get_google_maps_meta() : '' ;
//print_r(pronamic_get_google_maps_meta());
$pgm_map_type = ( $post != null ) ? get_post_meta( $post->ID, '_pronamic_google_maps_map_type', true ) : '';
$pgm_zoom = ( $post != null ) ? get_post_meta( $post->ID, '_pronamic_google_maps_zoom', true ) : '';
$pgm_address = ( $post != null ) ? get_post_meta( $post->ID, '_pronamic_google_maps_address', true ) : '';
$pgm_latitude = ( $post != null ) ? get_post_meta( $post->ID, '_pronamic_google_maps_latitude', true ) : '';
$pgm_longitude = ( $post != null ) ? get_post_meta( $post->ID, '_pronamic_google_maps_longitude', true ) : '';
wp_nonce_field('save-post', Pronamic_Google_Maps::NONCE_NAME);
?>
<div id="pronamic-google-maps-meta-box" >
<li>
<input id="pgm-map-type-field" name="<?php echo Pronamic_Google_Maps_Post::META_KEY_MAP_TYPE; ?>" value="<?php echo esc_attr( $pgm_map_type ); ?>" type="hidden" />
<input id="pgm-zoom-field" name="<?php echo Pronamic_Google_Maps_Post::META_KEY_ZOOM; ?>" value="<?php echo esc_attr( $pgm_zoom ); ?>" type="hidden" />
<input id="pgm-active-field" name="<?php echo Pronamic_Google_Maps_Post::META_KEY_ACTIVE; ?>" value="true" type="hidden" />
<label for="pgm-address-field">Address</label>
<textarea id="pgm-address-field" name="<?php echo Pronamic_Google_Maps_Post::META_KEY_ADDRESS; ?>" rows="2" cols="40"><?php echo esc_attr( $pgm_address ); ?></textarea>
<p class="description">Please type the address and click on "Geocode ↓" to find the location.</p>
</li>
<li>
<input id="pgm-geocode-button" type="button" value="<?php _e('Geocode ↓', 'pronamic_google_maps'); ?>" class="button" name="pgm_geocode" />
<input id="pgm-reverse-geocode-button" type="button" value="<?php echo _e('Reverse Geocode ↑', 'pronamic_google_maps'); ?>" class="button" name="pgm_reverse_geocode" />
</li>
<li>
<label for="pgm-lat-field">Latitude</label>
<input id="pgm-lat-field" name="<?php echo Pronamic_Google_Maps_Post::META_KEY_LATITUDE; ?>" value="<?php echo esc_attr($pgm_latitude); ?>" type="text" style="width: 200px;" />
°
</li>
<li>
<label for="pgm-lng-field">Longitude</label>
<input id="pgm-lng-field" name="<?php echo Pronamic_Google_Maps_Post::META_KEY_LONGITUDE; ?>" value="<?php echo esc_attr($pgm_longitude); ?>" type="text" style="width: 200px;" />
°
</li>
<li>
<label for="pgm-canvas">Location result</label>
<div id="pgm-canvas" style="width: 600px; height: 350px; margin-left: 140px; border: 1px solid white;"></div>
<p class="description">Tip: Change the zoom level and map type to your own wishes.</p>
</li>
</div>
<?php
}
add_action( 'wpuf_add_post_form_tags', 'wpufe_gmaps', 10, 2 );
Notes:
– For a reason I can’t figure out, the function pronamic_get_google_maps_meta()
didn’t worked so I needed to get the custom fields data using classic WP function get_post_meta;
– Since in front end you don’t use metaboxes, I forgot first to put the form between <div id="pronamic-google-maps-meta-box" ></div>
tags. This is important because the admin.js script will only work for this element id.
– Of course you have to update the custom fields with another function using update_post_meta()
for the pronamic custom fields. i.e.
update_post_meta( $post_id, '_pronamic_google_maps_map_type', $_POST['_pronamic_google_maps_map_type'] );
– I would like the posts to have the ggmap active by default, so active custom field is set “true” by default and hidden
Thanks a lot Remco for such a very good plugin.