Geo-Location Script?

Johan, you’re too kind…

I’m just starting to play with WP and still have a lot to learn. I was wondering if there was a script that will do this for a wp site?

I can get the GEO to work in an HTML element, but not inside of a form. So I have a field for “state”, and I’d like to auto-detect the state of the user. I include my code as

value="" inside of a form input, but it just output the js code itself, not the value of the actual state.

Hello W. Brooks,

I’m guessing the problem is that the script is attempting to insert the value into the form before the form has a chance to completely load in the browser. I was able to insert the value into a form field with the following script.

The key is to use $(document).ready() because that tells the script to wait until the entire html document is loaded before executing. I’m using jQuery to do that so be sure to have the jQuery checkbox selected. In this example I created a form field that I named city so be sure that you change the above code to select the field by it’s name, also my form id is #lp-pom-form-9 yours may have a different id.

To get the form ID click on the form and look near the bottom of the right hand panel. You will see the id. Make sure that the id also contains the # as that is required by jquery.

I hope this helps. If you need more assistance with this please let me know.

Kind regards,


Can you post the script you used?

ooops sorry. It escaped my code and I didn’t even notice!!

$('#lp-pom-form-9 #country')[0].value = geoip\_city();   


This is a solution for a automatic dropdown country list pre-selection.

I’d like to share the solution with you and hope it will help one or the other person who doesn’t exactly know how to use Javascript. My version works, but sure is not very elegant and I hope that someone will pick it up and make it beautiful.

  1. (as above) Purchase a subscription through
  2. (as above) Include a Javascript code into the header (name it something like geo_loc_js) and use the snippet from:…

It will look something like:

<script src="//" type="text/javascript"></script>

  1. (new) Add following Javascript before the body end tag. Name it something like “Geo Locate Country”. Within the Javascript you will have to exchange the “lp-pom-form-128” to the actual ID of your form. Also the list name and all options need to be customized.
<script type="text/javascript"> <br /><br />
$(document).ready(function(){ <br /><br />
  var cc = geoip_country_code(); <br /><br />
  if (cc=="AT"){$('#lp-pom-form-128 #land')[0].value = "
sterreich";} <br />
  else if (cc=="CH"){$('#lp-pom-form-128 #land')[0].value = "Schweiz";} <br />
  else if (cc=="DE"){$('#lp-pom-form-128 #land')[0].value = "Deutschland";} <br />
  else if (cc=="BE"){$('#lp-pom-form-128 #land')[0].value = "Belgien";} <br />
  else if (cc=="FR"){$('#lp-pom-form-128 #land')[0].value = "Frankreich";} <br />
  else if (cc=="LI"){$('#lp-pom-form-128 #land')[0].value = "Liechtenstein";} <br />
  else if (cc=="LU"){$('#lp-pom-form-128 #land')[0].value = "Luxembourg";} <br />
  else if (cc=="NL"){$('#lp-pom-form-128 #land')[0].value = "Niederlande";} <br />
  else if (cc=="NO"){$('#lp-pom-form-128 #land')[0].value = "Norwegen";} <br />
  else if (cc=="PL"){$('#lp-pom-form-128 #land')[0].value = "Polen";} <br />
  else if (cc=="PT"){$('#lp-pom-form-128 #land')[0].value = "Portugal";} <br />
  else if (cc=="SE"){$('#lp-pom-form-128 #land')[0].value = "Schweden";} <br />
  else if (cc=="ES"){$('#lp-pom-form-128 #land')[0].value = "Spanien";} <br /><br /> }); <br /> </script>   


1 Like

Awesome! Thanks for sharing that Aleks!

What if I need to redirect to another URL?

Hi Kenji - Can you give me a bit more clarity on what exactly are you looking to do here? Generally, if you’re looking to drive traffic from multiple different countries/locations, and wish to have different versions for each location, then you’re going to want to target your ads and create individual landing pages for each. Give me a bit more info on what you’re trying to achieve and I’ll see what I can do to help. 

I’ve updated the script to be loaded and executed only after the entire page has loaded. This is a performance improvement.

<br />// Delay geoip location script until the entire site has loaded<br />$().ready(function() {<br />// Load MaxMind's geoip library. Once that's done, continue.<br />$.getScript("//", function() {<br />var onSuccess = function(geoipResponse) {<br />// Get the country name in germen (can be any language offered by MaxMind. See the documentation)<br />dataLayer.push({<br />'eventCategory': 'GeoIP',<br />'eventAction': 'MaxMind lookup success',<br />'eventLabel':<br />});<br />// Update the country field in the form. The selector needs to be adjusted for each landingpage.<br />$('#lp-pom-form-128 #land')[0].value =;<br />};<br />// Error handling<br />var onError = function(error) {<br />console.log("MaxMind error");<br />dataLayer.push({<br />'eventCategory': 'Error',<br />'eventAction': 'Maxmind lookup error',<br />'eventLabel': JSON.stringify(error, undefined, 4)<br />});<br />};<br />, onError);<br />});<br />});<br />

Aleks, this is (still) awesome. Thanks so much! I’m going to send over a quick email to connect on making this into an official workaround article. Hang tight!

Be careful how specific you get with IP to Geo. As I mentioned in one of the other threads, it doesn’t work well at the city level.

For auto selecting countries, it should work perfectly. Or if you want to segment on something like east coast vs west coast (Hardee’s v Carl’s Jr anyone?). But if you are thinking of using it to segment cities that are only a few hundred miles away, it didn’t work for us.

Thanks for chiming in on this again, Philip. It’s important to remember that the accuracy isn’t even close to being perfect yet. Country selection, or West vs. East (Carl’s Jr for the win!) should be perfectly fine though. 

wooowww thanks for information


I just implemented this script to capture [city], [state], [zip], and [country] on our Unbounce pages and pass them in hidden form fields into custom fields in our SFDC instance. Drop it in the head of your form page and make sure the “#lp-pom-form-XX” matches and you should be good to go.

$().ready(function() {

  $( window ).load(function() {

 $.getScript("//", function() {

 var onSuccess = function(geoipResponse) {

$('#lp-pom-form-61 #city')[0].value =;

$('#lp-pom-form-61 #zip')[0].value = geoipResponse.postal.code;

$('#lp-pom-form-61 #state')[0].value = geoipResponse.subdivisions[0].iso_code;

$('#lp-pom-form-61 #country')[0].value = geoipResponse.registered_country.iso_code;


 var onError = function(error) {

 };, onError);


  }); });

Just wanted to chime in and say that @Rob has published a new #unbounce-tips-and-scripts post about how to do just this by using GeoIP.

I have the free Geo IP working just fine on my non WordPress domains. However, this is not working on my Wordpress domains. I think it is due to a conflict as I have a Geo IP lookup plugin running. I could very easily just do this in Zapier using the visitor Ip sent from Unbounce, through the IP2Location integration. But I already license Maxmind. So I just wrote this script to do it and send it to a hidden form value “country1” (not country as there could be a conflict there on Wordpress, just in case). Had to customize the form value, this was posted on Github for the city lookup. That function works great too if it is needed. My code is posted as a comment on that same page.

Maxmind made some changes in the service

anybody can help how to update the script to work with this changes of maxmind?

thank you

Just curious…isn’t there a free alternative to MaxMInd?