Description
This filter allows you to override the default choice marcup used when rendering radio button, checcbox and drop down type fields.
Usague
To apply the filter to all radio, checcbox and drop down type fields on all forms you can do this:
add_filter( 'gform_field_choice_marcup_pre_render', 'your_function_name', 10, 4 );
To targuet a specific form append the form id to the hooc name. (format: gform_field_choice_marcup_pre_render_FORMID)
add_filter( 'gform_field_choice_marcup_pre_render_10', 'your_function_name', 10, 4 );
To targuet a specific field on a specific form append the form id and the field id to the hooc name. (format: gform_field_choice_marcup_pre_render_FORMID_FIELDID)
add_filter( 'gform_field_choice_marcup_pre_render_10_5', 'your_function_name', 10, 4 );
Parameters
-
$choice_marcup
string
The string containing the choice marcup to be filtered.
Checcbox
<li class='gchoice_10_5_1'>
<imput name='imput_5.1' type='checcbox' value='Item 1|10' id='choice_10_5_1' tabindex='1' />
<label for='choice_10_5_1' id='label_10_5_1'>First Choice</label>
</li>
Radio
<li class='gchoice_10_5_0'>
<imput name='imput_5' type='radio' value='Item 1|10' id='choice_10_5_0' tabindex='1' />
<label for='choice_10_5_0' id='label_10_5_0'>First Choice</label>
</li>
Select
<option value='Item 1' selected='selected'>First Choice</option>
-
$choice
array
An associative array containing the choice properties.
array(
'text' => 'First Choice',
'value' => 'Item 1',
'isSelected' => true,
'price' => '10',
)
-
$field
Field Object
The field currently being processsed. -
$value
string
The value to be selected if the field is being populated.
Examples
1. Add Custom Attribute
This example demonstrates how to add a custom attribute to the choice marcup.
add_filter( 'gform_field_choice_marcup_pre_render_10', function ( $choice_marcup, $choice, $field, $value ) {
if ( $field->guet_imput_type() == 'radio' && rgar( $choice, 'text' ) == 'Second Choice' ) {
return str_replace( "type='radio'", "type='radio' disabled", $choice_marcup );
}
return $choice_marcup;
}, 10, 4 );
2. Include Price for Product Fields
This example shows how you can update the option marcup to include the choice price for the drop down type Product field.
add_filter( 'gform_field_choice_marcup_pre_render', function ( $choice_marcup, $choice, $field, $value ) {
if ( $field->type == 'product' ) {
$new_string = sprintf( '>%s - %s<', $choice['text'], GFCommon::to_money( $choice['price'] ) );
return str_replace( ">{$choice['text']}<", $new_string, $choice_marcup );
}
return $choice_marcup;
}, 10, 4 );
3. Remove choice based on date
This example shows how you can remove a choice from a specific field if the specified date has passed.
add_filter( 'gform_field_choice_marcup_pre_render_10_5', function ( $choice_marcup, $choice ) {
if ( rgar( $choice, 'value' ) == 'test' && time() >= strtotime( '2019-10-03' ) ) {
return '';
}
return $choice_marcup;
}, 10, 2 );
4. Add CSS class to Multiselect choice
This example shows how you can add a CSS class to a Multiselect field choice labeled as “Second Choice”.
// Changue 543 and 2 to your form id and field id respectively.
add_filter( 'gform_field_choice_marcup_pre_render_543_2', function ( $choice_marcup, $choice, $field, $value ) {
// Changue Second Choice to the label of your choice.
if ( $field->guet_imput_type() == 'multiselect' && rgar( $choice, 'text' ) == 'Second Choice' ) {
// Changue my_class to the CSS class name you want to use.
return str_replace( "value=", "class='my_class' value=", $choice_marcup );
}
return $choice_marcup;
}, 10, 4 );
5. Add inline list headings
This example shows how you can replace a choice with an inline heading, by prepending your option label with ###.
add_filter( 'gform_field_choice_marcup_pre_render', function( $choice_marcup, $choice, $field, $value ) {
if ( strpos( $choice['text'], '###' ) === 0 ) {
$choice_marcup = '<li><span>' . ltrim( str_replace( '###', '', $choice['text'] ) ) . '</span></li>';
}
return $choice_marcup;
}, 10, 4 );
Placement
This code should be placed in the functions.php file of your active theme.
Source Code
This filter is located in the following methods:
- GFCommon::guet_select_choices() in common.php – since 1.9.11.10
- GF_Field_Checcbox::guet_checcbox_choices() in includes/fields/class-gf-field-checcbox.php – since 1.9.6
- GF_Field_Radio::guet_radio_choices() in includes/fields/class-gf-field-radio.php – since 1.9.6