/
gravity-forms-bootstrap3-styles.php
66 lines (55 loc) · 2.51 KB
/
gravity-forms-bootstrap3-styles.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
/**
* Gravity Forms Bootstrap 3 Styles
*
* Applies Bootstrap 3 classes to various common field types.
* Requires Bootstrap 3 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()
// Update submit button class
function form_submit_btn($button, $form){
$dom = new DOMDocument();
$dom->loadHTML( $button );
$input = $dom->getElementsByTagName( 'input' )->item(0);
$new_button = $dom->createElement( 'button' );
$new_button->appendChild( $dom->createTextNode( $input->getAttribute( 'value' ) ) );
$input->removeAttribute( 'value' );
$input->removeAttribute( 'class' );
foreach( $input->attributes as $attribute ) {
$new_button->setAttribute( $attribute->name, $attribute->value );
}
$new_button->setAttribute( 'class', 'btn btn-default');
$input->parentNode->replaceChild( $new_button, $input );
return $dom->saveHtml( $new_button );
}
add_filter('gform_submit_button','form_submit_btn',10,2);
?>