Add a dropdown field to your form, and under “Advanced Options” add “dynamic-state” as a CSS class. Then add the following code to your theme’s functions.php file or core functionality plugin.
/**
* State Dropdown
*
*/
function ea_state_dropdown( $field, $form_data ) {
if( 'select' !== $field['type'] )
return $field;
$classes = explode( ' ', $form_data['fields'][ $field['id'] ]['css'] );
if( ! in_array( 'dynamic-state', $classes ) )
return $field;
$field['choices'] = array();
$states = wpforms_us_states();
foreach( $states as $value => $label )
$field['choices'][] = array( 'label' => $label, 'value' => $value );
return $field;
}
add_filter( 'wpforms_field_data', 'ea_state_dropdown', 20, 2 );