Skip to content

Instantly share code, notes, and snippets.

@kirandash
Created July 14, 2016 10:00
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save kirandash/a7549b8af7768d336730efc75edf188c to your computer and use it in GitHub Desktop.
Save kirandash/a7549b8af7768d336730efc75edf188c to your computer and use it in GitHub Desktop.
Helpful function for adding Bootstrap classes to Gravity Forms fields.
<?php
/**
* Gravity Forms Bootstrap Styles
*
* Applies bootstrap classes to various common field types.
* Requires Bootstrap to be in use by the theme.
*
* Using this function allows use of Gravity Forms default CSS
* in conjuction with Bootstrap (benefit for fields types such as Address).
*
* @see gform_field_content
* @link http://www.gravityhelp.com/documentation/page/Gform_field_content
*
* @return string Modified field content
*/
add_filter("gform_field_content", "bootstrap_styles_for_gravityforms_fields", 10, 5);
function bootstrap_styles_for_gravityforms_fields($content, $field, $value, $lead_id, $form_id){
// Currently only applies to most common field types, but could be expanded.
if($field["type"] != 'hidden' && $field["type"] != 'list' && $field["type"] != 'multiselect' && $field["type"] != 'checkbox' && $field["type"] != 'fileupload' && $field["type"] != 'date' && $field["type"] != 'html' && $field["type"] != 'address') {
$content = str_replace('class=\'medium', 'class=\'form-control medium', $content);
}
if($field["type"] == 'name' || $field["type"] == 'address') {
$content = str_replace('<input ', '<input class=\'form-control\' ', $content);
}
if($field["type"] == 'textarea') {
$content = str_replace('class=\'textarea', 'class=\'form-control textarea', $content);
}
if($field["type"] == 'checkbox') {
$content = str_replace('li class=\'', 'li class=\'checkbox ', $content);
$content = str_replace('<input ', '<input style=\'margin-left:1px;\' ', $content);
}
if($field["type"] == 'radio') {
$content = str_replace('li class=\'', 'li class=\'radio ', $content);
$content = str_replace('<input ', '<input style=\'margin-left:1px;\' ', $content);
}
return $content;
} // End bootstrap_styles_for_gravityforms_fields()
add_filter("gform_submit_button", "form_submit_button", 10, 2);
function form_submit_button($button, $form){
return "<button class='button btn btn-default' id='gform_submit_button_{$form["id"]}'><span>Submit</span></button>";
}
?>
@arleym
Copy link

arleym commented Feb 19, 2024

This is so old that probably no one else is experimenting anymore, but: Heads up that "submit" is hard coded on the buttons, breaking the ability to change the text in the current version of GF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment