bookmark_bordergform_us_states

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;
}

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