Using Gravity Forms ‘gform_predefined_choices’ PHP filter

The gform_predefined_choices Gravity Forms filter allows you to add new predefined choices or delete existing ones for selection fields (Checkboxes, Multiple Choice, and Drop Down).

On this pageJump to a section

Usage

To 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:

add_filter('gform_predefined_choices_5', 'your_function_name');

Parameters

  • $choices (array): An associative array of existing predefined choices to be filtered. The key is the title and the value is an array containing the choices.

More information

See Gravity Forms Docs: gform_predefined_choices

Examples

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

Add a new choice with values

This example adds a new predefined choice incorporating choices that have a value different from 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;
}

Add U.S. State Codes as Choice Values

This example updates 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;
}

Note: This filter is located in form_detail.php.

Leave a Comment

Your email address will not be published. Required fields are marked *

Type guide in lowercase to submit your comment.