One of the most rewarding things about working with WordPress is finding elegant ways to connect the front end with real-world business logic. Recently, I built a feature for a client that automatically routes customer service requests to the correct branch of the company — based entirely on the postcode entered in a Gravity Form.
Here’s a breakdown of how I did it using Gravity Forms, Advanced Custom Fields (ACF), and some clean PHP logic.
The Problem: Which Branch Covers This Area?
The client has multiple branches across the UK, each covering a specific set of postcode areas — like EH12
, AB10
, G1
, etc. When a customer submits a service request through the form on their website, the business needs that request to be automatically forwarded to the branch responsible for that postcode.
Manual routing wasn’t an option. It had to be smart, scalable, and easy to manage.
The Stack
- WordPress (custom theme)
- Gravity Forms for form handling
- Advanced Custom Fields (ACF) to attach postcode coverage info to taxonomy terms
- Custom PHP logic to handle the postcode matching and data routing
The Data Structure
Each branch is stored as a custom taxonomy term (location
) in WordPress. Each term has an ACF repeater field called postcode_area_coverage
, and inside that repeater, a subfield named code
holds the outward postcode areas like EH12
, G1
, AB10
.
So the structure looks like this:
location (taxonomy term)
└── postcode_area_coverage (repeater)
├── code: EH1
├── code: EH2
└── code: EH12
The Gravity Form
The form collects the usual user details, including a postcode field. Behind the scenes, I validate the format of the postcode, extract the outward part (everything before the space), and match it against the postcode coverage of each branch.
Once matched, I automatically store the matched branch in a hidden Gravity Forms field. That field can then be used to:
- Route notifications
- Display confirmation messages
- Track analytics
Extracting the Outward Code from a Full Postcode
if (preg_match('/^([A-Z]{1,2}[0-9][0-9A-Z]?)/', strtoupper(trim($postcode)), $matches)) {
$outward = $matches[1]; // e.g. "EH12" from "EH12 5AB"
}
This snippet takes the full UK postcode submitted in the form and extracts the outward code (first part). That’s what I use to match against each branch’s coverage.
Matching the Postcode to a Branch via ACF Repeater
$terms = get_terms(['taxonomy' => 'location', 'hide_empty' => false]);
foreach ($terms as $term) {
$coverage = get_field('postcode_area_coverage', 'location_' . $term->term_id);
foreach ($coverage as $row) {
if (strtoupper($row['code']) === $outward) {
return $term->term_id; // Match found
}
}
}
Here, I loop through each location
taxonomy term, check their ACF postcode coverage, and find a match with the user’s outward postcode. Once matched, I return that branch’s ID.
Updating a Gravity Form Field After Submission
GFAPI::update_entry_field($entry['id'], self::LOCATION_SELECTED_FROM_POSTCODE, $branch_id);
After finding the correct branch, I updated a hidden field in the submitted Gravity Form entry so it stores which branch should handle the request. This value is later used to route notifications.
At the End
- The business now handles customer requests faster and more efficiently.
- Postcode coverage can be updated by non-technical users directly in the WordPress admin.
- The system is scalable — just add a new location with postcode areas, and it’s instantly supported.
It’s always fun to bridge the gap between user input and smart business logic. This was a great example of how custom taxonomy, ACF, and Gravity Forms can work together to create something genuinely useful — with no need for heavyweight plugins or complex integrations.
Leave a Reply