Description
This filter is executed when the form editor is loaded, before creating the list of predefined choices for the selection fields (Checkboxes, Multiple Choice and Drop Down). This hook can be used to add new predefined choices as well as deleting existing ones.
Usage
This would apply the function to all forms.
add_filter( 'gform_predefined_choices', 'your_function_name' );
To limit the scope of the function to a specific form simply append the form id to the end of the hook name. (format: gform_predefined_choices_FORMID)
add_filter( 'gform_predefined_choices_5', 'your_function_name' );
Parameters
- $choices array
An array with the existing predefined choices to be filtered. It is an associative array where the key is the title and the value is an array containing the choices.
$choices['My Favorite Food'] = array( 'Fruit', 'Hamburger', 'Beans' );
Examples
1. Add a new choice
This example adds a new predefined choice to the end of the list.
add_filter( 'gform_predefined_choices', 'add_predefined_choice' ); function add_predefined_choice( $choices ) { $choices['My New Choice'] = array( 'Choice 1', 'Choice 2', 'Choice 3' ); return $choices; }
2. Add a new choice with values
This example adds a new predefined choice incorporating choices that have a value that is different than the choice label.
add_filter( 'gform_predefined_choices', 'add_predefined_choice' ); function add_predefined_choice( $choices ) { $choices['My New Choice'] = array( 'Choice 1|One', 'Choice 2|Two', 'Choice 3|Three' ); return $choices; }
3. Add U.S. State Codes as Choice Values
This example shows how you can update the choices for the U.S. states to include the state code as the choice value.
add_filter( 'gform_predefined_choices', 'update_us_states' ); function update_us_states( $choices ) { foreach ( $choices['U.S. States'] as &$state ) { $state .= '|' . GF_Fields::get( 'address' )->get_us_state_code( $state ); } return $choices; }
Source Code
This filter is located in form_detail.php.