WPAllImport is a fantastic tool for importing any external data you may have. I’ve used it in various projects to load shop items, restaurants, cars and hotels.
One major problem is that addresses are commonly non-standard. The most common element you will receive is the postcode. So, using this postcode, it should be possible to obtain the region for the item in question. In fact, it should be possible to obtain the hierarchical region.
There are other techniques – notable – Google have a reverse GeoCode service which can take a postcode or latitude+longitude co-ordinates and return a standardised address, but even using this can be hit and miss saying nothing of the daily limits imposed and the poor performance speed.
So – in one of my recent projects, I have written a little script which you can use in conjunction with WPAllImport to generate hierarchical location data based on the UK postcode. This means you can then offer browsable views or hierarchical menus to your users.
Here is a quick guide to implementing this using WPAllImport:
Create a new postcode locations lookup function
Add this function to your website – commonly this goes into functions.php in your theme folder, but locations may differ depending on the theme. e.g. in Themify themes, you can place it into custom-functions.php.
function getLocationHierarchy($city, $postcode) { $postcodetokens = explode(' ', $postcode); $locations = array( "AL" => "East Anglia > Hertfordshire > St. Albans", "CB" => "East Anglia > Cambridgeshire > Cambridge", "CM" => "East Anglia > Essex > Chelmsford", "CO" => "East Anglia > Essex > Colchester", "EN" => "East Anglia > Middlesex > Enfield", "IG" => "East Anglia > Essex > Ilford and Barking", "IP" => "East Anglia > Suffolk > Ipswich", "LU" => "East Anglia > Bedfordshire > Luton", "MK" => "East Anglia > Buckinghamshire > Milton Keynes", "NR" => "East Anglia > Norfolk > Norwich", "PE" => "East Anglia > Cambridgeshire > Peterborough", "RM" => "East Anglia > Essex > Romford", "SG" => "East Anglia > Hertfordshire > Stevenage", "SS" => "East Anglia > Essex > Southend-on-Sea", "WD" => "East Anglia > Hertfordshire > Watford", "B" => "The Midlands > West Midlands > Birmingham", "CV" => "The Midlands > West Midlands > Coventry", "DE" => "The Midlands > Derbyshire > Derby", "DY" => "The Midlands > West Midlands > Dudley", "LE" => "The Midlands > Leicestershire > Leicester", "NG" => "The Midlands > Nottinghamshire > Nottingham", "NN" => "The Midlands > Northamptonshire > Northampton", "ST" => "The Midlands > Staffordshire > Stoke-on-Trent", "WS" => "The Midlands > West Midlands > Walsall", "WV" => "The Midlands > West Midlands > Wolverhampton", "BD" => "North East England > West Yorkshire > Bradford", "DH" => "North East England > County Durham > Durham", "DL" => "North East England > North Yorkshire > Darlington", "DN" => "North East England > South Yorkshire > Doncaster", "HD" => "North East England > West Yorkshire > Huddersfield", "HG" => "North East England > North Yorkshire > Harrogate", "HU" => "North East England > North Humberside > Hull", "HX" => "North East England > West Yorkshire > Halifax", "LN" => "North East England > Lincolnshire > Lincoln", "LS" => "North East England > West Yorkshire > Leeds", "NE" => "North East England > Tyne and Wear > Newcastle", "S" => "North East England > South Yorkshire > Sheffield", "SR" => "North East England > Tyne and Wear > Sunderland", "TS" => "North East England > Cleveland > Teesside", "WF" => "North East England > West Yorkshire > Wakefield", "YO" => "North East England > North Yorkshire > York", "BB" => "North West England > Lancashire > Blackburn", "BL" => "North West England > Lancashire > Blackpool", "CA" => "North West England > Cumbria > Carlisle", "CW" => "North West England > Cheshire > Crewe", "FY" => "North West England > Lancashire> Blackpool", "L" => "North West England > Merseyside > Liverpool", "LA" => "North West England > Lancashire > Lancaster", "M" => "North West England > Lancashire > Manchester", "OL" => "North West England > Lancashire > Oldham", "PR" => "North West England > Lancashire > Preston", "SK" => "North West England > Cheshire > Stockport", "SY" => "North West England > Shropshire > Shrewsbury", "TF" => "North West England > Shropshire > Telford", "WA" => "North West England > Cheshire > Warrington", "WN" => "North West England > Lancashire > Wigan", "CH" => "North West England > Cheshire > Chester", "E" => "London > East London", "EC" => "London > City of London", "N" => "London > North London", "NW" => "London > North West London", "SE" => "London > South East London", "SW" => "London > South West London", "W" => "London > West End", "WC" => "London > West Central London", "GU" => "South Central England > Surrey > Guildford", "HA" => "South Central England > Middlesex > Harrow", "HP" => "South Central England > Hertfordshire > Hemel Hempstead", "OX" => "South Central England > Oxfordshire > Oxford", "PO" => "South Central England > Hampshire > Portsmouth", "RG" => "South Central England > Berkshire > Reading", "SL" => "South Central England > Buckinghamshire > Slough", "SN" => "South Central England > Wiltshire > Swindon", "SO" => "South Central England > Hampshire > Southampton", "SP" => "South Central England > Wiltshire > Salisbury", "UB" => "South Central England > Middlesex > Southall and Uxbridge", "BN" => "South East England > East Sussex > Brighton", "BR" => "South East England > Kent > Bromley", "CR" => "South East England > Surrey > Croydon", "CT" => "South East England > Kent > Canterbury", "DA" => "South East England > Kent > Dartford", "KT" => "South East England > Surrey > Kingston-upon-Thames", "ME" => "South East England > Kent > Medway", "RH" => "South East England > Surrey > Redhill", "SM" => "South East England > Surrey > Sutton", "TN" => "South East England > Kent > Tonbridge", "TW" => "South East England > Middlesex > Twickenham", "BA" => "South East England > Avon > Bath", "BH" => "South West England > Dorset > Bournemouth", "BS" => "South West England > Avon > Bristol", "DT" => "South West England > Dorset > Dorchester", "EX" => "South West England > Devon > Exeter", "GL" => "South West England > Gloucestershire > Gloucester", "HR" => "South West England > Herefordshire > Hereford", "PL" => "South West England > Devon > Plymouth", "TA" => "South West England > Somerset > Taunton", "TQ" => "South West England > Devon > Torquay", "TR" => "South West England > Cornwall > Truro", "WR" => "South West England > Worcestershire > Worcester", "CF" => "Wales > South Glamorgan, Cardiff", "LD" => "Wales > Powys > Llandrindod Wells", "LL" => "Wales > Clwyd > Llandudno", "NP" => "Wales > Gwent > Newport", "SA" => "Wales > West Glamorgan > Swansea", "BT" => "Northern Ireland > Belfast", "AB" => "Scotland > Aberdeen", "DD" => "Scotland > Angus > Dundee", "DG" => "Scotland > Dumfries and Galloway", "EH" => "Scotland > Midlothian > Edinburgh", "FK" => "Scotland > Stirlingshire > Falkirk", "G" => "Scotland > Lanarkshire > Glasgow", "HS" => "Scotland > Outer Hebrides > Isle Of Lewis", "IV" => "Scotland > Inverness", "KA" => "Scotland > Ayrshire > Kilmarnock and Ayr", "KW" => "Scotland > Caithness > Kirkwall", "KY" => "Scotland > Fife > Kirkcaldy", "ML" => "Scotland > Lanarkshire > Motherwell", "PA" => "Scotland > Renfrewshire > Paisley", "PH" => "Scotland > Perthshire > Perth", "TD" => "Scotland > Selkirkshire > Galashiels", "ZE" => "Scotland > Shetland Islands", "GY" => "Channel Islands > Guernsey", "JE" => "Channel Islands > Jersey", "IM" => "Isle Of Man"); $postcoderegionpart = str_replace(range(0,9), '', $postcodetokens[0]); $londonsuburbs = array( "W1" => "Mayfair and Oxford Street", "SW1" => "St James, Victoria and Westminster", "WC2" => "Covent Garden", "SW3" => "Knightsbridge & Chelsea", "SW7" => "Kensington", "W2" => "Marylebone", "E14" => "Canary Wharf", "WC1" => "Bloomsbury", "SE1" => "Southbank" ); $location = $locations[$postcoderegionpart]; if (!isset($location) || $location == null) { $location = $city; } else { if (array_key_exists($postcodetokens[0], $londonsuburbs)) { $location .= " > " . $londonsuburbs[$postcodetokens[0]]; } } return $location; }
Modify your WPAllImport options to call this postcode location lookup function. The function you’ve created above takes two parameters – the city and the postcode.
Here is how to modify your import to give you a hierarchical set of locations based on the UK postcode:
- Set the function call – e.g. getLocationHierarchy({city}, {postcode})] – just drag the relevant elements from your feed over to the parameters here for city and postcode
- Change the ‘Separated by’ option to ‘>’ and tick ‘Enable Auto Nest’ as in the above screenshot.
Now re-run your import and you will have UK regions extracted from your postcodes.
If you have any comments or wish to see this turned into a plugin, let me know.
If you wish to get WPAllImport, we highly recommend it, you can find out more and download it here.
- How to create a tree of categories from a custom taxonomy - September 22, 2014
- Managing postcodes and locations with WPAllImport - September 22, 2014
- Why do affiliate merchants reject your website? - August 20, 2014