mustardBees/cmb_field_map

Update latitude & longitude value based on custom field

Closed this issue · 5 comments

I'm sure you're busy, but thought you'd be the best person to ask for help! Hope that's okay!

I'm trying to take the CMB_Field_Map and tie it into a Gravity Forms. I've created a form where the user has the ability to drop a pin onto a map. Those map coordinates are then passed into a new post (set to draft/pending review of course). I've confirmed that Gravity forms is passing the value of the input to the appropriate entry in the DB (location_latitude & location_longitude), as the front end map tied to cmb_field_map shows the new entry immediately.

The issue I'm having is that unfortunately, in the post editor of the recently submitted map coordinates, the cmb_field_map doesn't update the pin according to the custom field location_latitude and location_longitude.

I've changed "hidden" to "text" on the lat / long inputs in cmb_field_map.php, and they too aren't being populated with the custom field location_latitude and location_longitude.

Is there any way to have those hidden fields along with the editor map check to see if the custom fields tied to them have been changed, and show the coordinates based on the location_latitude and location_longitude field, rather than simply being empty?

I'm assuming the google map would have to check the hidden (lat/long) fields and update based on those fields, and those fields would have to get the $wpdb entry for the custom fields. Unfortunately my backend tweaks are pretty limited, so any help or points in the right direction would be greatly appreciated!

Thanks again for the great plugin!

Wild shot in the dark… you say your meta keys are "location_latitude" and "location_longitude". When you register the pw_map field, does your id include a prefix?

If that's not the issue, get in touch as it may be easier to debug with some code to look over.

Originally I had a prefix, but unfortunately I had to drop it in order for Gravity Forms to be able to pick it up. For whatever reason when meta keys had the preceding underscore it wouldn't be picked up so I had to remove it in order to get them to play nice together. (see: http://wordpress.org/support/topic/plugin-gravity-forms-custom-post-types-custom-fields-not-showing-in-form)

If I was to find a way to include the prefix and get GF to work with these meta fields would it start updating based on meta values? Or is something else needed to make it all jive.

Thanks again for taking the time to look this over!

My map field saves three custom fields:

  1. An array containing both the latitude and longitude
  2. The latitude
  3. The longitude

You should be able to setup Gravity Forms to save the two values separately. However, my map field uses the array containing both to display the map in the WordPress admin area.

I don't have a copy of Gravity Forms to hand. However, from their documentation, it looks possible to save the latitude/longitude values in the correct format. I've written a snippet which you may be able to use as a starting point.

I forgot to mention that custom fields which have keys starting with an underscore are hidden by WordPress. Gravity Forms may also be hiding these, making it difficult to select an existing custom field name.

When you register the CMB field, you could just remove the prefix variable, like so:

array(
    'name' => 'Location',
    'desc' => 'Drag the marker to set the exact location',
    'id' => 'location',
    'type' => 'pw_map',
),

This would then give you custom field names of location_latitude and location_longitude which you should be able to select in Gravity Forms.

Thanks so much! I'll give this a try over the weekend, appreciate the help.