The same ward picker works on forms from other plugins and themes — contact forms, booking forms, custom checkouts. WooCommerce is not needed.
With data attributes
- Call
vafw_enqueue_picker()on the page that shows the form. - Mark the ward input with
data-vafw-wardand point it to the province field.
<?php vafw_enqueue_picker(); ?>
<select name="province" id="my-province">
<option value="HCM">TP. Hồ Chí Minh</option> …
</select>
<input name="ward"
data-vafw-ward
data-vafw-province="#my-province"
data-vafw-code="#my-ward-code">
<input type="hidden" name="ward_code" id="my-ward-code">
| Attribute | Meaning |
|---|---|
data-vafw-ward | This input is a ward field |
data-vafw-province | Selector of the province field; its value is a key (HCM) or a code (79) |
data-vafw-code | Optional: selector of a field that receives the 5-digit code |
data-vafw-country | Optional: selector of a country field; the picker only opens when its value is VN |
Check again on the server. The picker helps people choose; it is not a security check. Validate the submitted ward with vafw_find_ward() and store the code.
JavaScript API
For fields added after the page loads, or for your own interface, use window.vafw:
| Method | What it does |
|---|---|
vafw.attach( input, { province, country, code } ) | Turns an input into a ward picker; options are elements or CSS selectors |
vafw.wards( province ) | Promise of [ { code, name } ] for a province |
vafw.find( province, text ) | Promise of the official ward { code, name } or null — same rules as the server |
vafw.rescan() | Looks for new data-vafw-ward inputs |
vafw.find( 'HCM', 'ben thanh' ).then( function ( ward ) {
console.log( ward ); // { code: "26743", name: "Phường Bến Thành" }
} );