Skip to content

Instantly share code, notes, and snippets.

@dabernathy89
Created March 13, 2013 19:55
Show Gist options
  • Star 19 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save dabernathy89/5155551 to your computer and use it in GitHub Desktop.
Save dabernathy89/5155551 to your computer and use it in GitHub Desktop.
Insert this script into functions.php in your WordPress theme (be cognizant of the opening and closing php tags) to allow field groups in Gravity Forms. The script will create two new field types - Open Group and Close Group. Add classes to your Open Group fields to style your groups. Note that there is a stray (but empty) <li> element created. …
<?php
/*
Insert this script into functions.php in your WordPress theme (be cognizant of the opening and closing php tags) to allow field groups in Gravity Forms. The script will create two new field types - Open Group and Close Group. Add classes to your Open Group fields to style your groups.
Note that there is a stray (but empty) <li> element created. It is given the class "fieldgroup_extra_li" so that you can hide it in your CSS if needed.
*/
add_filter("gform_add_field_buttons", "add_fieldgroup_fields");
function add_fieldgroup_fields($field_groups){
foreach($field_groups as &$group){
if($group["name"] == "standard_fields"){
$group["fields"][] = array("class"=>"button", "value" => __("Open Group", "gravityforms"), "onclick" => "StartAddField('fieldgroupopen');");
$group["fields"][] = array("class"=>"button", "value" => __("Close Group", "gravityforms"), "onclick" => "StartAddField('fieldgroupclose');");
break;
}
}
return $field_groups;
}
// Add title to the Field Group fields
add_filter( 'gform_field_type_title' , 'field_group_titles' );
function field_group_titles( $type ) {
if ( $type == 'fieldgroupopen' ) {
return __( 'Open Field Group' , 'gravityforms' );
} else if ( $type == 'fieldgroupclose' ) {
return __( 'Close Field Group' , 'gravityforms' );
}
}
add_filter("gform_field_content", "create_gf_field_group", 10, 5);
function create_gf_field_group($content, $field, $value, $lead_id, $form_id){
if ( ! is_admin() ) {
if(rgar($field,"type") == "fieldgroupopen"){
$content = "<ul><li style='display: none;'>";
}
else if(rgar($field,"type") == "fieldgroupclose"){
$content = "</li></ul><!-- close field group --><li style='display: none;'>";
}
}
return $content;
}
// Add a CSS class to the Field Group Close field so we can hide the extra <li> that is created.
add_action("gform_field_css_class", "close_field_group_class", 10, 3);
function close_field_group_class($classes, $field, $form){
if($field["type"] == "fieldgroupclose"){
$classes .= " fieldgroup_extra_li";
}
return $classes;
}
add_action("gform_editor_js_set_default_values", "field_group_default_labels");
function field_group_default_labels(){
?>
case "fieldgroupopen" :
field.label = "Field Group Open";
break;
case "fieldgroupclose" :
field.label = "Field Group Close";
break;
<?php
}
add_action("gform_editor_js", "allow_fieldgroup_settings");
function allow_fieldgroup_settings(){
?>
<script type='text/javascript'>
fieldSettings["fieldgroupopen"] = fieldSettings["text"] + ", .cssClass";
fieldSettings["fieldgroupclose"] = fieldSettings["text"] + ", .cssClass";
</script>
<?php
}
?>
@eokoneyo
Copy link

Hi this is really nice, would it be possible to add the functionality for allowing the users duplicate the particular group field when filling the form has the need arises.

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