Last active
May 14, 2020 04:49
-
-
Save richardW8k/8404630 to your computer and use it in GitHub Desktop.
move a Gravity Forms radio input inside the label
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// move radio input inside label | |
add_filter("gform_field_choices", "radio_input_inside_label", 10, 2); | |
function radio_input_inside_label($choices, $field){ | |
if($field["type"] != "radio") | |
return $choices; | |
$choices = ""; | |
if(is_array($field["choices"])){ | |
$choice_id = 0; | |
$count = 1; | |
$logic_event = !empty($field["conditionalLogicFields"]) ? "onclick='gf_apply_rules(" . $field["formId"] . "," . GFCommon::json_encode($field["conditionalLogicFields"]) . ");'" : ""; | |
foreach($field["choices"] as $choice){ | |
$id = $field["id"] . '_' . $choice_id++; | |
$field_value = !empty($choice["value"]) || rgar($field, "enableChoiceValue") ? $choice["value"] : $choice["text"]; | |
$checked = rgar($choice,"isSelected") ? "checked='checked'" : ""; | |
$tabindex = GFCommon::get_tabindex(); | |
$input = sprintf("<input name='input_%d' type='radio' value='%s' %s id='choice_%s' $tabindex $logic_event />", $field["id"], esc_attr($field_value), $checked, $id); | |
$choices .= sprintf("<li class='gchoice_$id'><label for='choice_%s'>%s %s</label></li>", $id, $choice["text"], $input); | |
$count++; | |
} | |
} | |
return $choices; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I was having trouble getting this working. I needed it to work with the Gravity Forms Quiz addon. So I changed up the code to so that it would work with that, but then required questions were not being saved in multipage form.
In the end I just rearranged the normal $choices output instead of trying to rebuild the entire thing using $fields. It worked out to be less code also.
Hope this helps anyone else looking for a solution: