Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Apply Bootstrap 4 custom checkboxes and radio buttons to Gravity Fields generated forms
<?php
// Apply Bootstrap 4 custom checkboxes and radio buttons to Gravity Fields generated forms
// Derived from https://jayhoffmann.com/using-gravity-forms-bootstrap-styles/
add_filter( 'gform_field_container', 'add_bootstrap_control_class', 10, 6 );
function add_bootstrap_control_class( $field_container, $field, $form, $css_class, $style, $field_content ) {
$id = $field->id;
$field_id = is_admin() || empty( $form ) ? "field_{$id}" : 'field_' . $form['id'] . "_$id";
$control = '';
if (strpos($field_content, 'ginput_container_checkbox') > 0) {
$control = 'checkbox';
} elseif (strpos($field_content, 'ginput_container_radio') > 0) {
$control = 'radio';
}
// find the control class, add custom-control custom-checkbox/radio to it
$to_find = "gchoice_";
$new_content = str_replace($to_find, "custom-control custom-{$control} " . $to_find, $field_content);
// did it match?
if ($field_content != $new_content) {
// add the classes to the label and the control
$patterns = array();
$patterns[0] = "/(id='choice_)/";
$patterns[1] = "/(id='label_)/";
$replacements = array();
$replacements[0] = "class='custom-control-input' $1";
$replacements[1] = "class='custom-control-label' $1";
$new_content = preg_replace($patterns, $replacements, $new_content);
}
return '<li id="' . $field_id . '" class="' . $css_class . '">' . $new_content . '</li>';
}
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.