Hi Stiofan.
Sorry to bother again and again. I have a little problem to solve, and I hope you can help.
Basically, I would like to include London borough within the address, but do it programmatically. So, I have created a custom text field ‘borough’ and set it as Admin only edit.
There is this resource https://postcodes.io where you can get all location details from a postcode. So, if you search SW1A 2AA, you get json:
{
"status": 200,
"result": {
"postcode": "SW1A 2AA",
"quality": 1,
"eastings": 530047,
"northings": 179951,
"country": "England",
"nhs_ha": "London",
"longitude": -0.127695,
"latitude": 51.50354,
"european_electoral_region": "London",
"primary_care_trust": "Westminster",
"region": "London",
"lsoa": "Westminster 018C",
"msoa": "Westminster 018",
"incode": "2AA",
"outcode": "SW1A",
"parliamentary_constituency": "Cities of London and Westminster",
"admin_district": "Westminster",
"parish": "Westminster, unparished area",
"admin_county": null,
"admin_ward": "St James's",
"ced": null,
"ccg": "NHS Central London (Westminster)",
"nuts": "Westminster",
"codes": {
"admin_district": "E09000033",
"admin_county": "E99999999",
"admin_ward": "E05000644",
"parish": "E43000236",
"parliamentary_constituency": "E14000639",
"ccg": "E38000031",
"ccg_id": "09A",
"ced": "E99999999",
"nuts": "UKI32"
}
}
}
I am after ‘admin_district’, so I am trying this snippet, which doesn’t work:
add_filter('wp_insert_post_data','_add_borough',10,2);
function _add_borough($data,$postarr){
if(isset($data['post_type']) && function_exists('geodir_get_posttypes') && in_array($data['post_type'],geodir_get_posttypes()) && isset($data['post_title'])) {
if(isset($postarr['zip']) && !empty($postarr['ID'])) {
$postcode = $postarr['zip'];
$jsonurl = "https://api.postcodes.io/postcodes/".$postcode;
$json = $this->request($jsonurl);
$decoded = json_decode($json);
if($decoded->status == 200){
$data['borough'] = $decoded->result->admin_district;
}
}
}
return $data;
}
What am I doing wrong?
Many thanks in advance,
Eugene