gform_append_field_choice_option

Description

This filter can be used to add custom options for each choice.

Usage

gform.addFilter('gform_append_field_choice_option', function (str, field, i) {
// define the markup for your custom option
return str;
});

Parameters

  • str string

    An empty string, to be replaced with your custom markup.

  • field array

    The current field object.

  • i integer

    The index of the current choice.

Example

This example adds a new option to the choice setting called custom.

It uses the gform_editor_js action to load the code snippet on the form editor page.

add_action( 'gform_editor_js', 'custom_option_editor_script' );
function custom_option_editor_script() {
?>
<script type='text/javascript'>
jQuery('.choices_setting')
.on('input propertychange', '.field-choice-custom', function () {
var $this = jQuery(this);
var i = $this.closest('li.field-choice-row').data('index');

field = GetSelectedField();
field.choices[i].custom = $this.val();
});
gform.addFilter('gform_append_field_choice_option', function (str, field, i) {
var inputType = GetInputType(field);
var custom = field.choices[i].custom ? field.choices[i].custom : '';

return "<input type='text' id='" + inputType + "_choice_custom_" + i + "' value='" + custom + "' class='field-choice-input field-choice-custom' />";
});
</script>
<?php
}

Source Code

This filter is located in js.php