Using Gravity Forms ‘gform_chained_selects_input_choices’ PHP filter

The gform_chained_selects_input_choices filter is used to modify the choices that appear in each drop down of a Chained Select field.

Usage

To filter all chained selects for all forms:

add_filter('gform_chained_selects_input_choices', 'your_function_name');

To filter all chained selects for a specific form:

add_filter('gform_chained_selects_input_choices_6', 'your_function_name');

To filter chained selects for a specific form and specific field:

add_filter('gform_chained_selects_input_choices_6_2', 'your_function_name');

To filter a specific chained select for a specific form and field:

add_filter('gform_chained_selects_input_choices_6_2_1', 'your_function_name');

Parameters

  • $input_choices (array) – An array of Choices that populate the current drop down.
  • $form_id (integer) – The form ID for the current form.
  • $field (Field Object) – The current field object.
  • $input_id (float) – The input ID of the current input (i.e. drop down) being processed. Example: 3.1.
  • $full_chain_value (array) – An array of values representing the selected value for each input in the chain of selects. Example:
array(
    '3.1' => 'Toyota',
    '3.2' => 'Matrix',
    '3.3' => 2013
);
  • $value (string) – The value of the current chained select.
  • $index (int) – The index of the specific chained select.

More information

See Gravity Forms Docs: gform_chained_selects_input_choices Important: This filter will not work properly when the page where your form is embedded is cached. This is not a Gravity Forms limitation but a consequence of using caching.

Examples

Define the default choice

Set the second choice as the default choice in the third drop down of field 1 on form 123:

add_filter('gform_chained_selects_input_choices_123_1_3', function($choices) {
    $choices[1]['isSelected'] = true;
    return $choices;
});

Populate a drop down based on previous selections

Populate a chained select drop down based on the selected values in the previous drop downs. This example uses an API to get the data.

// Populate the "Category" drop down of our Chained Select
add_filter('gform_chained_selects_input_choices_5_3_1', 'gf_populate_categories', 10, 7);

function gf_populate_categories($input_choices, $form_id, $field, $input_id, $chain_value, $value, $index) {
    // Your custom code to fetch the categories
    // e.g. $categories = your_api_get_categories();

    // Add each category to the choices array
    $choices = array();
    foreach ($categories as $category) {
        $choices[] = array(
            'text' => $category['name'],
            'value' => $category['id'],
            'isSelected' => false
        );
    }

    return $choices;
}

// Populate the "Subcategory" drop down of our Chained Select
add_filter('gform_chained_selects_input_choices_5_3_2', 'gf_populate_subcategories', 10, 7);

function gf_populate_subcategories($input_choices, $form_id, $field, $input_id, $chain_value, $value, $index) {
// Get the selected category from the previous drop down
$selected_category = $chain_value["{$field->id}.1"];

if (!$selected_category) {
    return $input_choices;
}

// Your custom code to fetch the subcategories based on the selected category
// e.g. $subcategories = your_api_get_subcategories($selected_category);

// Add each subcategory to the choices array
$choices = array();
foreach ($subcategories as $subcategory) {
    $choices[] = array(
        'text' => $subcategory['name'],
        'value' => $subcategory['id'],
        'isSelected' => false
    );
}

return $choices;
}

// Populate the "Item" drop down of our Chained Select
add_filter('gform_chained_selects_input_choices_5_3_3', 'gf_populate_items', 10, 7);

function gf_populate_items($input_choices, $form_id, $field, $input_id, $chain_value, $value, $index) {
// Get the selected subcategory from the previous drop down
$selected_subcategory = $chain_value["{$field->id}.2"];

if (!$selected_subcategory) {
    return $input_choices;
}

// Your custom code to fetch the items based on the selected subcategory
// e.g. $items = your_api_get_items($selected_subcategory);

// Add each item to the choices array
$choices = array();
foreach ($items as $item) {
    $choices[] = array(
        'text' => $item['name'],
        'value' => $item['id'],
        'isSelected' => false
    );
}

return $choices;
}

Change the text and value of a drop down choice

Change the text and value of the first choice in the second drop down of field 4 on form 7:

add_filter('gform_chained_selects_input_choices_7_4_2', function($choices) {
    $choices[0]['text'] = 'New Choice Text';
    $choices[0]['value'] = 'new_choice_value';
    return $choices;
});

Remove a specific choice from a drop down

Remove the choice with the value “not_needed” from the first drop down of field 2 on form 8:

add_filter('gform_chained_selects_input_choices_8_2_1', function($choices) {
    foreach ($choices as $key => $choice) {
        if ($choice['value'] === 'not_needed') {
            unset($choices[$key]);
            break;
        }
    }
    return $choices;
});

Add a custom choice to a drop down

Add a custom choice to the third drop down of field 6 on form 9:

add_filter('gform_chained_selects_input_choices_9_6_3', function($choices) {
    $choices[] = array(
        'text' => 'Custom Choice',
        'value' => 'custom_choice',
        'isSelected' => false
    );
    return $choices;
});