Today I’ve written a short tutorial to show you how to leverage pods framework pre-save filter in order to automatically get address coordinates using Google geocoding functionalities.

Prerequisites:

As I stated in previous post – most of my tutorials code will be hosted on GitHub to allow me (and hopefully you too) to update all of the code snippets in more efficient manner. At the same time I would like to paste the code here too for all non-github users and not to force you to jump between sites. I will try to keep both versions the same but please head to my GitHub repository for the newest version of the snippet.

How to use it?

  1. Add two fields to your pod – one for the address and second one for automatically generated coordinates.
  2. Go to my GitHub repository or copy code from down below.
  3. Put the code into your theme’s functions.php file or to your custom UI plugin.
  4. Replace %%podname%%, %%address_field_name_here%%, %%coordinates_field_name_here%% with the proper values (your pod name, name of your address field, name of your coordinates field).
  5. And that should be it.

I have few address fields in my pod, what now?

If you keep address separated in your pod then you can still use the code with just slight modification:

Please remember about removing the if statement in such case (the one checking if the address field exists). Also it would be good to sanitize user input to be sure that our request url is nice and clean

Why there are two filters in the code?

If you take a closer look into the code you will see that I’ve used two filters in there. The first one is responsible for our core functionality and calls nice lady from Google to get coordinates from her ;). The second one is just small function which uses a little bit dirty code to make coordinates field disabled – so the user will not be able to modify the values.

Full filter code

Related posts

Posted by Kamil Grzegorczyk Owner / Development

  • HughPatersonIII

    Really, cool and helpful. Thank you. I suppose, if I am reading the code correctly that the coordinates data gets stored in the custom field as ‘lat,lon’. How would I alter the code if I also wanted to store lat and lon in their own fields? So, three fields 1. ‘lat,lon’, 2. ‘lat’, 3. ‘lon’ I know that this is redundant in the database, but I have several different processes (plugins, etc) going on which are pulling from different arrangements.

    • Kamil Grzegorczyk

      Hey – You can write and structure your code as you need, in the way which fits your project in a best possible manner. You can either store lat / lon separately or combine them in one field but I rather not repeat that data twice in DB

  • Pingback: Wordpress GIS Posts | NetPath.info()

Like what you see?

Contact us today and get started!