Add a Country and State Dropdown to a WordPress Form
A country dropdown is easy to paste into a WordPress page. Keeping a second field in sync is harder: if a visitor changes the country from the United States to Canada, California should disappear and Canadian provinces should become available. CountriesDB maintains the lists and updates the second dropdown when the first changes.
The website form guide walks through the shared snippet and account setup.
What you need
Use a form whose HTML and submission handler you control. This approach covers a Custom HTML block containing your own form or a form rendered by your theme. WPForms, Gravity Forms and Contact Form 7 handle their own fields and submissions. This guide uses the standalone widget in custom markup.
Create a CountriesDB project, generate a public key and add the hostname where the form is published to the domain allowlist.
Set up the fields in WordPress
1. Open the form markup
In the block editor, use a Custom HTML block for a form you own. If your theme renders the form, insert the fields in that form's template. The WordPress Custom HTML documentation explains which permissions control whether scripts survive saving. If the editor strips the script, ask a site administrator to load it through the theme.
2. Add the widget and fields
Place the selects inside your existing form, with field names your handler expects. Load the script once on the page and replace the placeholder with the public key. Keep the existing submit button and handler.
<script src="https://unpkg.com/@countriesdb/widget@latest/dist/index.js?public_key=YOUR_PUBLIC_KEY"></script>
<label for="country">Country</label>
<select id="country" name="country" class="country-selection"
data-name="country" data-label="Select country" data-preselected="" required></select>
<label for="region">State / province / region</label>
<select id="region" name="region" class="subdivision-selection"
data-country="country" data-label="Select region"></select>
3. Publish and send a test submission
Open the published page on the allowed hostname, pick a country and region, then switch countries and check that the old region clears. Submit a test and inspect the values in the destination, such as your CRM. If the dropdowns stay empty, check that the published HTML still includes the script and the hostname is allowed.
FAQ
Do I need a developer?
You can paste the fields yourself if you already control the markup and its submission handler. Ask a developer for help if the theme owns the form or the handler needs new fields.
Will it load states and provinces?
Yes, where the widget has subdivision data. Use a broad label such as “State / province / region” for an international form.
Will the block send email or save leads?
Keep your form's existing submission handler for that. The widget supplies the choices for the two selects.
Can I check the submitted country and region codes?
Use the Backend Validation API from your server and store the private key there.