I’m hoping there’s a code ninja lurking who can tell me what’s wrong with my code.
I’m trying to rewrite the output for checkbox and radio fields. This form uses the User Registration add-on and updates custom user preferences.
My code works… it rewrites the output for checkboxes or radios BUT… it’s not saving selections. Here’s the code. Comments follow.
function rewrite_gravity_forms_switch_output( $field_content, $field ) {
if ( is_page() && ( 'checkbox' == $field->type || 'radio' == $field->type ) ) {
$field_content = "";
$choices = $field->choices;
$choice_number = 1;
foreach( $choices as $choice ) {
if ( $choice_number % 10 == 0 ) {
$choice_number ++;
}
$input_name = 'input_' . $field['id'] . '.' . $choice_number; // ex --> input_17.2
if( 'checkbox' == $field->type ) {
if( 'toggle' == ! $field->adminLabel ) {
$field_content .= sprintf( '<label class="switch"><input type="checkbox" name="%s" value="%s" class="switch" %s /><span class="checkmark"></span><span class="label">%s</span></label>', $input_name, $choice['value'], $choice['isSelected'], $choice['text'] );
}
else {
$field_content .= sprintf( '<label class="switch toggle"><input type="checkbox" name="%s" value="%s" class="switch toggle" %s /><span class="checkmark toggle"></span><span class="label">%s</span></label>', $input_name, $choice['value'], $choice['isSelected'], $choice['text'] );
}
}
else {
$field_content .= sprintf( '<label class="switch radio"><input type="radio" name="%s" value="%s" class="switch radio" %s /><span class="checkmark radio"></span><span class="label">%s</span></label>', 'input_'.$field['id'], $choice['value'], $choice['isSelected'], $choice['text'] );
}
$choice_number++;
} // end foreach
} // end if check/radio
return $field_content;
}
add_filter( 'gform_field_content', 'rewrite_gravity_forms_switch_output', 10, 2 );
Again, this code produces the HTML I need, but fails to save the selected values. I’m guessing GF needs something else I’m not adding? I didn’t bother to add all the ids and classes cuz I just don’t need them but maybe GF needs them to save properly?
There is another filter called gform_field_choice_markup_pre_render that I could use as well. I did try that at one point but encountered the same issues.
Any illumination here would be much appreciated.