Description
This filter can be used to modify the choices listed in the US states drop down.
Usage
add_filter( 'gform_us_states', 'your_function_name' );
Parameters
- $states array
The array to be filtered. It contains the list of states in a standard array (see sample below).
array( 'Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', ... );
Examples
1. Use State Code
This example demonstrates how to use the two letter state code as the choice value.
add_filter( 'gform_us_states', 'us_states' );
function us_states( $states ) {
$new_states = array();
foreach ( $states as $state ) {
$new_states[ GF_Fields::get( 'address' )->get_us_state_code( $state ) ] = $state;
}
return $new_states;
}
2. Remove States
add_filter( 'gform_us_states', 'filter_us_states' );
function filter_us_states( $states ) {
$omit_states = array(
'Alaska',
'Hawaii'
);
foreach ( $states as $key => $state ) {
if ( in_array( $state, $omit_states ) ) {
unset( $states[ $key ] );
}
}
return $states;
}
3. Add Outlying Territories to US State List
add_filter( 'gform_us_states', 'us_states' );
function us_states( $states ) {
$territories = array(
2 =>'American Samoa',
12=>'Guam',
37 =>'Northern Mariana Islands',
42 =>'Puerto Rico',
48 =>'United States Minor Outlying Islands',
49 =>'U.S. Virgin Islands'
);
foreach ( $territories as $key => $t ) {
array_splice( $states, $key, 0, $t );
}
return $states;
}
Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This filter is located in GF_Field_Address::get_us_states() in includes/fields/class-gf-field-address.php.