Documentation

Dynamically generate List Dropdown choices

The ‘gravitywp_list_dropdown_choices’ filter allows you to adapt or define the list dropdown choices.

Usage

add_filter( 'gravitywp_list_dropdown_choices', 'my_custom_fuction', 10,6 );

Parameters

  • $drop_down_choices – Array of dropdown choices each with a ‘value’ and a ‘text’ (label) property.
  • $field – The Field Object.
  • $column – The column name (label).
  • $value – The fields value as serialized string.
  • $form_id – The form id.

Example

This example populates the List Field Dropdown column ‘Select Product’ for field with ID 1 and form with ID 514 with 3 products.

/**
 * Function: gravitywp_list_dropdown_choices.
 *
 * @param array    $drop_down_choices Array of dropdown choices.
 * @param GF_Field $field             The Field Object.
 * @param string   $column            The column name (label).
 * @param string   $value             The fields value as serialized string.
 * @param int      $form_id           The form id.
 *
 * @return array
 */
function gravitywp_list_dropdown_choices( $drop_down_choices, $field, $column, $value, $form_id ) {
    if ( $form_id == 514 && $field->id == 1 && $column == 'Select Product' ) {
        $drop_down_choices = array(
            array(
                'value' => 'p1',
                'text'  => 'Product 1',
            ),
            array(
                'value' => 'p2',
                'text'  => 'Product 2',
            ),
            array(
                'value' => 'p3',
                'text'  => 'Product 3',
            ),
        );
    }
    return $drop_down_choices;
}
add_filter( 'gravitywp_list_dropdown_choices', 'gravitywp_list_dropdown_choices', 10,6 );

Placement

You can place this code in your themes functions.php.

List Dropdownv2.1.1

For this functionality to work you need to install the GravityWP - List Dropdown Add-on.

Read more