WPsites Home
WordPress : state-of-the-art content management system Login to your account

Gravity Forms – adding UK specific address fields

gravityforms

I haven’t used Gravity Forms much, as Contact Form 7 is our form plugin of choice, but today a client wanted to have the address changed on one of his Gravity forms. Just State changing to county and zip removing from the post code section.

On Gravity Forms the individual address fields are not that easy to change since the whole address field set is a single object, with a couple of options such as hiding address line 2. To change the address fields you need to put together some PHP to hook deep within the Gravity Form code and alter the output.

Here is the code that you will need to place in your themes functions.php file:


add_filter("gform_address_types", "uk_address", 10, 2);
function uk_address($address_types, $form_id){
$address_types["uk"] = array(
"label" => "UK",
"country" => "United Kingdom",
"zip_label" => "Postcode",
"state_label" => "County",
"states" => array("Aberdeenshire"=>"Aberdeenshire","Angus/Forfarshire"=>"Angus/Forfarshire","Argyllshire"=>"Argyllshire","Ayrshire"=>"Ayrshire","Banffshire"=>"Banffshire","Bedfordshire"=>"Bedfordshire","Berkshire"=>"Berkshire",
"Berwickshire"=>"Berwickshire","Blaenau Gwent"=>"Blaenau Gwent","Bridgend"=>"Bridgend","Buckinghamshire"=>"Buckinghamshire","Buteshire"=>"Buteshire","Caerphilly"=>"Caerphilly","Caithness"=>"Caithness",
"Cambridgeshire"=>"Cambridgeshire","Cardiff"=>"Cardiff","Carmarthenshire"=>"Carmarthenshire","Ceredigion"=>"Ceredigion","Cheshire"=>"Cheshire","Clackmannanshire"=>"Clackmannanshire",
"Conwy"=>"Conwy","Cornwall"=>"Cornwall","Cromartyshire"=>"Cromartyshire","Cumberland"=>"Cumberland","Denbighshire"=>"Denbighshire","Derbyshire"=>"Derbyshire","Devon"=>"Devon","Dorset"=>"Dorset",
"Dumfriesshire"=>"Dumfriesshire","Dunbartonshire/Dumbartonshire"=>"Dunbartonshire/Dumbartonshire","Durham"=>"Durham","East Lothian/Haddingtonshire"=>"East Lothian/Haddingtonshire","Essex"=>"Essex","Fife"=>"Fife",
"Flintshire"=>"Flintshire","Gloucestershire"=>"Gloucestershire","Gwynedd"=>"Gwynedd","Hampshire"=>"Hampshire","Herefordshire"=>"Herefordshire","Hertfordshire"=>"Hertfordshire","Huntingdonshire"=>"Huntingdonshire",
"Inverness-shire"=>"Inverness-shire","Isle of Anglesey"=>"Isle of Anglesey","Kent"=>"Kent","Kincardineshire"=>"Kincardineshire","Kinross-shire"=>"Kinross-shire","Kirkcudbrightshire"=>"Kirkcudbrightshire",
"Lanarkshire"=>"Lanarkshire","Lancashire"=>"Lancashire","Leicestershire"=>"Leicestershire","Lincolnshire"=>"Lincolnshire","Merthyr Tydfil"=>"Merthyr Tydfil","Middlesex"=>"Middlesex",
"Midlothian/Edinburghshire"=>"Midlothian/Edinburghshire","Monmouthshire"=>"Monmouthshire","Morayshire"=>"Morayshire","Nairnshire"=>"Nairnshire","Neath Port Talbot"=>"Neath Port Talbot",
"Newport"=>"Newport","Norfolk"=>"Norfolk","Northamptonshire"=>"Northamptonshire","Northumberland"=>"Northumberland","Nottinghamshire"=>"Nottinghamshire","Orkney"=>"Orkney",
"Oxfordshire"=>"Oxfordshire","Peeblesshire"=>"Peeblesshire","Pembrokeshire"=>"Pembrokeshire","Perthshire"=>"Perthshire","Powys"=>"Powys","Renfrewshire"=>"Renfrewshire",
"Rhondda Cynon Taff"=>"Rhondda Cynon Taff","Ross-shire"=>"Ross-shire","Roxburghshire"=>"Roxburghshire","Rutland"=>"Rutland","Selkirkshire"=>"Selkirkshire","Shetland"=>"Shetland","Shropshire"=>"Shropshire",
"Somerset"=>"Somerset","Staffordshire"=>"Staffordshire","Stirlingshire"=>"Stirlingshire","Suffolk"=>"Suffolk","Surrey"=>"Surrey","Sussex"=>"Sussex","Sutherland"=>"Sutherland","Swansea"=>"Swansea","Torfaen"=>"Torfaen",
"Vale of Glamorgan"=>"Vale of Glamorgan","Warwickshire"=>"Warwickshire","West Lothian/Linlithgowshire"=>"West Lothian/Linlithgowshire","Westmorland"=>"Westmorland",
"Wigtownshire"=>"Wigtownshire","Wiltshire"=>"Wiltshire","Worcestershire"=>"Worcestershire","Wrexham"=>"Wrexham","Yorkshire"=>"Yorkshire")
);
return $address_types;
}

Once that code is in place you will then have an additional address type called UK (default options are International, United States and Canadian).

Before

After

  • Sam Cranwell

    Awesome fix! Works a treat – can the label for the City field be changed too?

    I tried adding this into the php you wrote but nothing:

    “city_label” => “Town/City”,

    Cheers, Sam

    • Simon

      I can’t see a way to do that using the ‘gform_address_types’ filter as above.

      To modify the city label you’ll probably need to use the ‘gform_address_city’ filter. Returning the text you’d like it to be: ‘Town/City’

  • http://twitter.com/ZhoomMarketing Ciaran Mahoney

    Thanks for the code! I modified to suit Australia… Hope this helps someone else looking like I was! Cheers

    add_filter(“gform_address_types”, “aus_address”, 10, 2);
    function aus_address($address_types, $form_id){
    $address_types[“australia”] = array(
    “label” => “Australia”,
    “country” => “Australia”,
    “zip_label” => “Postcode”,
    “state_label” => “State”,
    “states” => array(“NT”=>”NT”, “ACT”=>”ACT”,”NSW”=>”NSW”, “QLD”=>”QLD”, “VIC”=>”VIC”, “WA”=>”WA”,”SA”=>”SA”, “TAS” => “TAS”)
    );
    return $address_types;
    }

    • Jen

      Hi and thanks for this… I have tried adding this into my functions.php file of my theme and every time I do i break it :( Can you tell me more specifically where it needs to be added in?

  • Matt

    Thanks for this – worked a treat! :)

  • Ryan

    Great article. To change ‘city’ to ‘town/city’ just add this code:

    add_filter(“gform_address_city”, “set_gravityform_city”);
    function set_gravityform_city($label){
    return “Town/City”;
    }

  • http://www.mark-making.com/ Shannon Young

    If you do not want to change anything else but the Postcode label do this.

    add_filter(“gform_address_zip”, “change_address_zip”, 10, 2);
    function change_address_zip($label, $form_id){
    return “Postcode”;
    }

  • Pheyonagh

    Thank you, Hiding the ‘state’ field was driving me mad.

  • Zeeshan Asif

    What if i would like to get rid of all fields from adress section rexpect state field ?

  • Connor Murray

    I have tried to add London into the code but it does not appear in the actual form, can anyone help?

  • ajwhite

    Hi

    I’ve been looking online for UK format GF-addon for a while. 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…