Home Forums Add-ons User Submitted HOW-TO: Update google map latitude/longitude after importing, using address


HOW-TO: Update google map latitude/longitude after importing, using address

    • radgh

    • September 25, 2015 at 5:54 am

    I’ve recently imported a ton of store locations into a WordPress website using the plugin “WP All Import”. Many of the fields were imported into Advanced Custom Fields, including the address.

    However, neither ACF nor WP All Import calculated the latitude/longitude coordinates for these locations during the import. (and they weren’t in the spreadsheet, which would have saved a ton of time!)

    Going through 1000+ entries by hand would have taken forever. So I built a script that runs through them. It’s semi-manual, you just have to refresh the page as you step through them. It’s not an extensive plugin, but it works.


    1. Grab the file from this Gist and upload it to your theme.

    2. Configure the options in the variable $ld_recalc. You MUST set the post type, and set the different field keys (see Finding the Field Key).

    3. Call the file at the top of your functions.php script using include "recalculate-acf-locations.php";

    4. Visit your site with this query var on the end of the url, eg:

    5. You should see a results screen like this.

    6. Refresh the page again to continue scanning locations. Note: Google Maps API will prevent you from scanning too quickly.


    Every time you visit the URL from Step 4, it will scan a certain amount of locations. If the lookup fails or it can’t save the results, it will abort and will scan the failed location again later.

    When a scan is successful, the lat/lng is saved to the given google_map field as well as to custom fields named “latitude” and “longitude”. A successfully scanned location is not scanned again until the “scan_identifier” changes.

  • Thanks for posting this how to, I’m sure it will come in handy for others that are trying to import map fields using All Import.

    • portegno

    • September 23, 2016 at 3:08 am

    hi radgh!

    I’m running into the same problem with Google Address importing, but I can’t use your script, it always appears this message:
    ACF Recalc Locations: All locations have latitude/longitude present! All done!

    But my 500+ records are still not updated. If I go to each record and update the record by hand it works ok, but I can’t update them using your script.

    I have updated the field for google map with my field id:
    ‘google_map’ => ‘field_56e9d8cb41509’,

    Do you have an idea of what I’m doing wrong?

    thanks for your help.


    • radgh

    • September 23, 2016 at 4:22 am

    Could you check the post type? It returns “All locations updated…” when the WP Query is empty. So if it happens immediately, it must not be finding any results.

    If your records aren’t publicly queryable you might need to change post_status to include draft, private, trash, etc. I recommend debugging the WP_Query object that is generated and try changing the parameters if needed. It’s just a script that is meant to be modified so don’t worry too much about getting your hands dirty.

    • portegno

    • September 23, 2016 at 4:58 am

    It’s working ok now, it was a typo on the CPT name.

    Thanks for your help and such a great script!!

  • This worked amazing about 2 months ago wicked work guys! I added some more locations to a site yesterday and this script no longer works. Same site, same code.

    It simply goes straight to the error:
    “Google Maps returned an empty response”
    “ERROR! Google map could not locate this address. Aborting operation.”

    This is odd as when I manually enter the address into the Google Map field it finds the location perfectly.

    I am wondering if there has been a change to the Google API or an update to the Google map field in ACF which is preventing this script for working as it did 2 months ago? I hope there could be a quick fix as we have 1500 locations to GeoCode

    • radgh

    • October 21, 2016 at 9:34 am

    @ashercharles Take a look at the line that includes “file_get_contents”. There’s a URL being passed in there. Echo that out and see if that URL gives you the error. That would probably be what you want to update. I’m not sure if they have changed anything or if this is just a bug. Try another location, too, maybe?

  • Just did some further testing and on my localhost, this script works fine however when it is run from the live testing server it doesn’t work, so looks like a server config issue, not an issue with the script.

    • radgh

    • October 21, 2016 at 9:45 am

    @ashercharles You might want to try modifying the code to use wp_remote_get. If I spent the time to properly maintain this code, I would have used it instead of file_get_contents.

    This may or may not help you with the issue. But good luck!

  • Looks like the testing server doesn’t allow “allow_url_include”

  • Thanks for the documentation, all fixed, maybe commit this to the Git, as I have it all working on the staging live server now.

    $json = file_get_contents(“$address&sensor=false”);

    $request = wp_remote_get(“$address&sensor=false”);
    $json = wp_remote_retrieve_body( $request );

    • radgh

    • October 21, 2016 at 10:37 am

    Thanks @ashercharles, I updated the gist with your edits.

    • jasonSP

    • November 29, 2016 at 10:19 pm

    Hi guys,

    i’ve used this script previously and it worked fantastically well, so firstly thank you very much for this as it’s saved me a lot of time of manually updating them.

    I’ve come back to use this script again with the updates kindly pointed out by @ashercharles but I can’t seem to get it to work anymore. When I try go to the ?acf-recalc-locations it hangs for a while like it’s going to do something then hits the homepage.

    I tried going directly to the file to see what’s wrong and I get the following:

    Fatal error: Call to undefined function add_action() in /var/sites/f/ on line 75

    The line in question is this one:

    add_action( ‘wp’, ‘recalc_acf_locations_init’ );

    Has anybody else had this problem?.

    Any help would be greatly appreciated.


    • radgh

    • November 30, 2016 at 5:42 am

    “Call to undefined function add_action” sounds like your script is not running through WordPress. Are you accessing your PHP file directly? You should be using a URL that loads your website normally, such as:

    And NOT like this:

    Also make sure the script is loaded in functions.php of your theme. add_action should be defined by the time it gets to it that way.

    • jasonSP

    • November 30, 2016 at 8:36 pm

    Hi radgh,

    Thanks for the response.

    I’ve done it in exactly the way you described,

    I’ve only renamed the recalc as I have another identical copy for another custom post type on the same theme.

    As soon as I try to run the script as you mention it just refreshes the homepage. I checked the tables within the db to see if it had updated them but it’s still not updating them. I remember the last time I used it that it had the white screen and it ran through all the locations with the all done text.

  • Thank you for the script, but it works a bit incorrect for me sending to the latitude and longitude fields correct coordinates, but not to the google map fields.

    For example, it inserted this:


    but the right way for this would be:

    what does this “d:” before the coordinate means? and why so loooong?

    • radgh

    • July 23, 2017 at 7:26 am

    @petyakh The code updates the database using an array with the lat/lng. It’s updated using ACF’s own update_field() function. So if it isn’t working, something might have changed.

    As for the “d:” I’m assuming that indicates the value is a decimal instead of an “s” for string. And it’s longer because it’s more accurate. And perhaps the Google Geocoding API is more (or less) accurate than the Google Maps API that ACF uses on the admin screen.

    So is it that the address name, “bla-bla-bla” in your example, is not being displayed on the map field when editing a post?

    • jakus

    • October 5, 2017 at 11:57 am

    This is a super helpful tool..

    Thanks very much for sharing @radgh!

Viewing 18 posts - 1 through 18 (of 18 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.