Using Gravity Forms ‘gform_field_map_choices’ PHP action

The gform_field_map_choices filter allows you to override the choices available in the field map dropdown.

Usage

add_filter('gform_field_map_choices', 'your_function_name', 10, 4);

Parameters

  • $fields (array) – The value and label properties for each choice.
  • $form_id (integer) – The ID of the form currently being configured.
  • $field_type (null|array) – Null or the field types to be included in the dropdown.
  • $exclude_field_types (null|array|string) – Null or the field type(s) to be excluded from the dropdown.

More information

See Gravity Forms Docs: gform_field_map_choices

Examples

Add new choice

Add a new choice to the field map dropdown.

add_filter('gform_field_map_choices', function($fields, $form_id, $field_type, $exclude_field_types) {
    $fields[] = array('label' => 'The new choice', 'value' => 'new_choice');
    return $fields;
}, 10, 4);

Place this code in the functions.php file of your active theme.

Filter choices by form ID

Filter the field map choices based on the form ID.

add_filter('gform_field_map_choices', function($fields, $form_id, $field_type, $exclude_field_types) {
    if ($form_id == 1) {
        $fields[] = array('label' => 'Form 1 choice', 'value' => 'form_1_choice');
    }
    return $fields;
}, 10, 4);

Include specific field types

Include only specific field types in the field map dropdown.

add_filter('gform_field_map_choices', function($fields, $form_id, $field_type, $exclude_field_types) {
    if (in_array($field_type, array('text', 'number'))) {
        $fields[] = array('label' => 'Text and number fields only', 'value' => 'text_number');
    }
    return $fields;
}, 10, 4);

Exclude specific field types

Exclude specific field types from the field map dropdown.

add_filter('gform_field_map_choices', function($fields, $form_id, $field_type, $exclude_field_types) {
    if (!in_array($field_type, array('checkbox', 'radio'))) {
        $fields[] = array('label' => 'Exclude checkbox and radio fields', 'value' => 'exclude_checkbox_radio');
    }
    return $fields;
}, 10, 4);

Modify existing choice label

Change the label of an existing choice in the field map dropdown.

add_filter('gform_field_map_choices', function($fields, $form_id, $field_type, $exclude_field_types) {
    foreach ($fields as &$field) {
        if ($field['value'] == 'existing_choice') {
            $field['label'] = 'Modified existing choice';
        }
    }
    return $fields;
}, 10, 4);