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; | |
} |
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;
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
`
// 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;
}`