-
-
Save richardW8k/8404630 to your computer and use it in GitHub Desktop.
<?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; | |
} |
`
// move checkbox input inside label
add_filter("gform_field_choices", "radio_input_inside_label", 10, 2);
function radio_input_inside_label($choices, $field){
if($field["type"] != "checkbox")
return $choices;
$choices = "";
if(is_array($field["choices"])){
$choice_id = 0;
$count = 1;
$logic_event = !empty($field["conditionalLogicFields"]) ? "onclick='gf_apply_rules(" . $field["formId"] . "," . 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='checkbox' value='%s' %s id='choice_%s' $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, $input, $choice["text"]);
$count++;
}
}
return $choices;
}`
I would love to use this, but I can't get it working. Have placed into child theme functions.php
but it doesn't change anything, and I can't see any problems in the code either.
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:
// Gravity Forms Quiz - 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"] == "quiz" || $field["type"] == "radio") || $field["imageChoices_enableImages"] == "true") { // Exit if it's not field type 'quiz' or 'radio', and is also not using the Image Choices Plugin. You might need to edit this part for your specific setup.
return $choices;
}
preg_match_all("/(<([\w]+)[^>]*>)(.*?)(<\/\\2>)/", $choices, $liArray); // Get the li elements
preg_match_all("~<input(.*?)\/>~", $choices, $inputArray); // Get the input elements
preg_match_all("~<label(.*?)>(.*?)<\/label>~", $choices, $labelArray); // Get the label elements
$choices = "";
// Rearrange and build the new $choices array
if(is_array($field["choices"])){
$count = 0;
foreach($liArray[1] as $openingLi){
$choices .= $openingLi . "<label" . $labelArray[1][$count] . ">" . $inputArray[0][$count] . $labelArray[2][$count] . "</label></li>";
$count++;
}
}
return $choices;
}
Awesome! I did some small tweaking with the output of this and it worked great for my needs. I am trying to apply the same technique for the checkboxes but with no luck. Have you found a solution for that as well?