Advanced Gravity Forms: UK addresses

Posted by Lloyd Jones
Last updated
UK Counties

Gravity Forms is great. It does everything I need for form-related stuff on websites, but doesn’t have UK address formats and counties added by default. Look here:

default_address_type

The only options for the Address field are International, United States and Canada.
This is a pain, as I am from the UK and most of my clients want UK counties  and address labels on their sites. Thankfully, the solution is simple. I’ve done the hard bit (gathering a list of all counties in the UK). All you have to do is paste this into your theme’s functions.php file:

add_filter("gform_address_types", "add_uk_counties", 10, 2);

function add_uk_counties($address_types, $form_id){
$address_types["uk"] = array( "label" => "United Kingdom", "country" => "United Kingdom", "zip_label" => "Postcode", "state_label" => "County", "states" => array( 
'Avon' => 'Avon', 'Bedfordshire' => 'Bedfordshire', 'Berkshire' => 'Berkshire', 'Buckinghamshire' => 'Buckinghamshire', 'Cambridgeshire' => 'Cambridgeshire', 'Cheshire' => 'Cheshire', 'Cornwall' => 'Cornwall', 'Cumberland' => 'Cumberland', 'Derbyshire' => 'Derbyshire', 'Devon' => 'Devon', 'Dorset' => 'Dorset', 'Durham' => 'Durham', 'East Yorkshire' => 'East Yorkshire', 'Essex' => 'Essex', 'Gloucestershire' => 'Gloucestershire', 'Hampshire' => 'Hampshire', 'Herefordshire' => 'Herefordshire', 'Hertfordshire' => 'Hertfordshire', 'Huntingdonshire' => 'Huntingdonshire', 'Kent' => 'Kent', 'Lancashire' => 'Lancashire', 'Leicestershire' => 'Leicestershire', 'Lincolnshire' => 'Lincolnshire', 'Middlesex' => 'Middlesex', 'Norfolk' => 'Norfolk', 'North Yorkshire' => 'North Yorkshire', 'Northamptonshire' => 'Northamptonshire', 'Northumberland' => 'Northumberland', 'Nottinghamshire' => 'Nottinghamshire', 'Oxfordshire' => 'Oxfordshire', 'Rutland' => 'Rutland', 'Shropshire' => 'Shropshire', 'Somerset' => 'Somerset', 'Staffordshire' => 'Staffordshire', 'Suffolk' => 'Suffolk', 'Surrey' => 'Surrey', 'Sussex' => 'Sussex', 'Warwickshire' => 'Warwickshire', 'West Yorkshire' => 'West Yorkshire', 'Westmorland' => 'Westmorland', 'Wiltshire' => 'Wiltshire', 'Worcestershire' => 'Worcestershire', 'Aberdeenshire' => 'Aberdeenshire', 'Angus/Forfarshire' => 'Angus/Forfarshire', 'Argyllshire' => 'Argyllshire', 'Ayrshire' => 'Ayrshire', 'Banffshire' => 'Banffshire', 'Berwickshire' => 'Berwickshire', 'Buteshire' => 'Buteshire', 'Cromartyshire' => 'Cromartyshire', 'Caithness' => 'Caithness', 'Clackmannanshire' => 'Clackmannanshire', 'Dumfriesshire' => 'Dumfriesshire', 'Dunbartonshire/Dumbartonshire' => 'Dunbartonshire/Dumbartonshire', 'East Lothian/Haddingtonshire' => 'East Lothian/Haddingtonshire', 'Fife' => 'Fife', 'Inverness-shire' => 'Inverness-shire', 'Kincardineshire' => 'Kincardineshire', 'Kinross-shire' => 'Kinross-shire', 'Kirkcudbrightshire' => 'Kirkcudbrightshire', 'Lanarkshire' => 'Lanarkshire', 'Midlothian/Edinburghshire' => 'Midlothian/Edinburghshire', 'Morayshire' => 'Morayshire', 'Nairnshire' => 'Nairnshire', 'Orkney' => 'Orkney', 'Peeblesshire' => 'Peeblesshire', 'Perthshire' => 'Perthshire', 'Renfrewshire' => 'Renfrewshire', 'Ross-shire' => 'Ross-shire', 'Roxburghshire' => 'Roxburghshire', 'Selkirkshire' => 'Selkirkshire', 'Shetland' => 'Shetland', 'Stirlingshire' => 'Stirlingshire', 'Sutherland' => 'Sutherland', 'West Lothian/Linlithgowshire' => 'West Lothian/Linlithgowshire', 'Wigtownshire' => 'Wigtownshire', 'Anglesey/Sir Fon' => 'Anglesey/Sir Fon', 'Brecknockshire/Sir Frycheiniog' => 'Brecknockshire/Sir Frycheiniog', 'Caernarfonshire/Sir Gaernarfon' => 'Caernarfonshire/Sir Gaernarfon', 'Carmarthenshire/Sir Gaerfyrddin' => 'Carmarthenshire/Sir Gaerfyrddin', 'Cardiganshire/Ceredigion' => 'Cardiganshire/Ceredigion', 'Denbighshire/Sir Ddinbych' => 'Denbighshire/Sir Ddinbych', 'Flintshire/Sir Fflint' => 'Flintshire/Sir Fflint', 'Glamorgan/Morgannwg' => 'Glamorgan/Morgannwg', 'Merioneth/Meirionnydd' => 'Merioneth/Meirionnydd', 'Monmouthshire/Sir Fynwy' => 'Monmouthshire/Sir Fynwy', 'Montgomeryshire/Sir Drefaldwyn' => 'Montgomeryshire/Sir Drefaldwyn', 'Pembrokeshire/Sir Benfro' => 'Pembrokeshire/Sir Benfro', 'Radnorshire/Sir Faesyfed' => 'Radnorshire/Sir Faesyfed', 'County Antrim' => 'County Antrim', 'County Armagh' => 'County Armagh', 'County Down' => 'County Down', 'County Fermanagh' => 'County Fermanagh', 'County Tyrone' => 'County Tyrone', 'County Londonderry/Derry' => 'County Londonderry/Derry'
)
);
return $address_types;
}

And then you’ll get this option:
uk-postcode-added

 

How exciting. Now, there is a “post code” label (no mention of ‘zips’), and a nice list of counties.

You can also (obviously) repeat this process for any other country (you’ll have to obtain a list of every county in your country).

I hope this is useful to someone.

6 Comments

AJ

Hi

I’ve been looking online for UK format GF-addon for a while. All the blog advice like this uses the same outdated list of counties. For a start several counties no longer exist (some haven’t since 1860). Others are simply not counties. Worst, several counties are not present. Massive counties like Merseyside. Is this because ONE PERSON copied some outdated list of counties off the net and everyone since then, simply copied that person?

Even the plugin addon in WP.org (GF uk address format) copies this same incorrect list of counties.

Such a seemingly simple thing, and a well-regarded plugin like GF – i am amazed that nobody has done it yet.

Gravity Forms themselves simply bat this back to the customer saying ‘everyone recognises international format’. Well not in the UK. Many people would not complete ‘city’ if they lived in a small town, others look at state/province/region and think ‘what?’. Believe me i run a letting agency and had people query it so i took it off and now have several ‘single line’ text fields instead.

It would be great if someone simply went on a professional website (a big UK company for example!) and copied the Royal Mail approved setup from there…

Reply
Lloyd

Hi Arlo,

Thanks for your comment & contribution. I found myself somewhat frustrated by the same issue, as I expected the UK addresses to work “out of the box” but discovered I had to add them in myself.

Your point re: the outdated list of counties is taken, but this site is made for coders as a programming resource, rather than an accurate list of UK counties. I’m really just trying to impart the principle of what to do, thereby allowing people like yourself to achieve their aims.

If you were able to obtain an accurate list of counties and send it my way (via this site’s Contact form), I’d happily update the blog post!

Cheers and thanks for reading,
Lloyd

Reply
Lloyd

Hey Ben,

No problem at all! Glad you like it and that it works for you!

Cheers,
Lloyd

Reply
JC

Hello,
This is very very useful thank you!
Just one question…
Do you know of, or can you help implement an UK address lookup by post code in Gravity forms?

It’s got a field for entering the post code, then they select the address as it comes up and there is “Enter Address Manually” option below displaying standard fields.

Thank you in advance!

Reply
Lloyd

My pleasure!

re: your question:

It can definitely be done, but isn’t “natively” supported (ie, it would be a custom job).

You would have to, off the top of my head, have an AJAX call to wherever you’ve got the address details stored (probably easier to use an address API like https://getaddress.io/), and then it returns the results instantly and populates the form.

Let me know if you’d like any more info’ on this.

Cheers,
Lloyd

Reply

Leave a Reply

Your email address will not be published. Required fields are marked *