Rename Zip and State labels based on users location (Country) using IP address

This is for fellow knacksters who have users from different countries and use the address field. This code takes the users IP address and determines what country they are in to change the terminology address labels for Zip and State.

If for example I’m in the UK ‘Zip’ changes to ‘Postcode’ and ‘State’ = ‘County’. If then via your IP address you are in another country then we can change the labels based on that country.

Apologies I have only added If in the UK but have added If else for you to use other destinations to the code.

$(document).on('knack-page-render.any', function() {
$.getJSON("", function(data) {
  var country = data.country_name;
  if (country === "United Kingdom") {
    $('[for="zip"]').text('Post Code')
  } else if (country === "United States") {
// Copy this for as many locations you want to add and change the name of the location
  } else if (country === "Netherlands") {
       $('[for="zip"]').text('Post Code')
  } else {
    // do something here if Your location could not be determined

If anyone needs help adding additional locations or you have any further ideas based on location then please add your comments below.


Another useful bolt on Johnny - thank you.

1 Like

Hey, I hope you’ve been enjoying these code snippets. I wanted to let you know that all of my code is now available on my new platform, Appknowledged. Appknowledged has taken over from KnackMods. I encourage all of you to sign up—it’s completely free—and you’ll have access to all of my free content.

1 Like