Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
move a Gravity Forms radio input inside the label
<?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;
}
@natedunn

This comment has been minimized.

Copy link

natedunn commented Nov 27, 2016

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?

@thommeredith

This comment has been minimized.

Copy link

thommeredith commented Mar 4, 2017

`
// 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;

}`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.