Hey! I’m trying to change the markup for the radio buttons on my form to a bootstrap button group using filters. I was able to get most of it, but there’s one thing I can’t figure out, so I’m using jQuery to solve for it, but I’d like to not use jQuery if at all possible. Here’s the Bootstrap setup for button groups:
<div class="btn-group" role="group">
<input type="radio" class="btn-check" name="btnradio" id="btnradio1" autocomplete="off">
<label class="btn btn-outline-primary" for="btnradio1">Radio 1</label>
<input type="radio" class="btn-check" name="btnradio" id="btnradio2" autocomplete="off">
<label class="btn btn-outline-primary" for="btnradio2">Radio 2</label>
</div>
And here’s where I’ve landed using filters:
<div class="ginput_container ginput_container_radio">
<ul class="gfield_radio" id="input_1_8">
<input name="input_8" type="radio" class="btn-check" autocomplete="off" value="Single" id="choice_1_8_0">
<label class="btn text-white btn-outline-primary" for="choice_1_8_0" id="label_1_8_0">
Single
</label>
<input name="input_8" type="radio" class="btn-check" autocomplete="off" value="Married" id="choice_1_8_1">
<label class="btn text-white btn-outline-primary" for="choice_1_8_1" id="label_1_8_1">
Married
</label>
</ul>
</div>
Right now, I’m using jQuery to change the ul
to <div class="btn-group" role="group">
, but I’d like to do that using a filter, if possible. I saw the gform_field_container
filter, and that only removes the surrounding div
… and also removes the surrounding div
from everything. When I tried doing it with if($field["type"] == 'radio')
, it removed everything that wasn’t a radio button and the surrounding div
on the radio button.
Here’s what I have to adjust the rest of the stuff:
add_filter( 'gform_field_choice_markup_pre_render_1', function ( $choice_markup, $choice, $field, $value ) {
if ( $field->get_input_type() == 'radio' ) {
$choice_markup = strip_tags($choice_markup, '<input><label>');
$choice_markup = str_replace( "type='radio'", "type='radio' class='btn-check' autocomplete='off'", $choice_markup );
$choice_markup = str_replace( "label for", "label class='btn text-white btn-outline-primary' for", $choice_markup );
}
return $choice_markup;
}, 10, 4 );
I know that’s a lot of text, so let me know if I need to clarify anything.