Picker on any form

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

  1. Call vafw_enqueue_picker() on the page that shows the form.
  2. Mark the ward input with data-vafw-ward and 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">
AttributeMeaning
data-vafw-wardThis input is a ward field
data-vafw-provinceSelector of the province field; its value is a key (HCM) or a code (79)
data-vafw-codeOptional: selector of a field that receives the 5-digit code
data-vafw-countryOptional: 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:

MethodWhat 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" }
} );