Gform add field buttons

Description

This filter can be used to add/edit/remove the "add field" buttons from the form editor's floating toolbox

Usage

<?php
add_filter("gform_add_field_buttons", "add_map_field");
?>

Parameters

$field_groups

(array) The array to be filtered. It contains the field groups (i.e. Standard Fields, Advanced Fields, etc...). Each group then has a "fields" array containing all the fields in the group. Below is how this array is defined:

<?php
$standard_fields = array(
array("class"=>"button", "value" => __("Single Line Text", "gravityforms"), "onclick" => "StartAddField('text');"),
array("class"=>"button", "value" => __("Paragraph Text", "gravityforms"), "onclick" => "StartAddField('textarea');"),
array("class"=>"button", "value" => __("Drop Down", "gravityforms"), "onclick" => "StartAddField('select');"),
..... 
);

$advanced_fields = array(
array("class"=>"button", "value" => __("Name", "gravityforms"), "onclick" => "StartAddField('name');"),
array("class"=>"button", "value" => __("Date", "gravityforms"), "onclick" => "StartAddField('date');"),
array("class"=>"button", "value" => __("Time", "gravityforms"), "onclick" => "StartAddField('time');"),
.......                                  
);
                                                  
$post_fields = array(
array("class"=>"button", "value" => __("Title", "gravityforms"), "onclick" => "StartAddField('post_title');"),
array("class"=>"button", "value" => __("Body", "gravityforms"), "onclick" => "StartAddField('post_content');"),
array("class"=>"button", "value" => __("Excerpt", "gravityforms"), "onclick" => "StartAddField('post_excerpt');"),
.....
);                                

$field_groups = array(
array("name" => "standard_fields", "label"=> __("Standard Fields", "gravityforms"), "fields" => $standard_fields, "tooltip_class" => "tooltip_bottomleft"),
array("name" => "advanced_fields", "label"=> __("Advanced Fields", "gravityforms"), "fields" => $advanced_fields),
array("name" => "post_fields", "label"=> __("Post Fields", "gravityforms"), "fields" => $post_fields)
);

?>

Examples

This example adds a "Map" field button to the advanced group


<?php
add_filter("gform_add_field_buttons", "add_map_field");
function add_map_field($field_groups){

    foreach($field_groups as &$group){
        if($group["name"] == "advanced_fields"){
            $group["fields"][] = array("class"=>"button", "value" => __("Map", "gravityforms"), "onclick" => "StartAddField('map');");
            break;
        }
    }
    return $field_groups;
}
?>


This example removes the file upload button and the entire post field group from the form editor toolbox


<?php

//----------------------------------------------------------------------------
//----- Removing fields from Form edit screen
//----------------------------------------------------------------------------

add_filter("gform_add_field_buttons", "remove_fields");
function remove_fields($field_groups){

    $index = 0;
    $post_field_index = -1;
    $advanced_field_index = -1;

    //Finding group indexes
    foreach($field_groups as $group){
        if($group["name"] == "post_fields")
            $post_field_index = $index;
        else if($group["name"] == "advanced_fields")
            $advanced_field_index = $index;

        $index ++;
    }

    //removing file upload field
    if($advanced_field_index >=0){
        $file_upload_index = -1;
        $index = 0;
        foreach($field_groups[$advanced_field_index]["fields"] as $advanced_field){
            if($advanced_field["value"] == "File Upload")
                $file_upload_index = $index;
            $index++;
        }

        unset($field_groups[$advanced_field_index]["fields"][$file_upload_index]);
    }

    //removing entire post field group
    if($post_field_index >= 0)
        unset($field_groups[$post_field_index]);

    return $field_groups;
}
?>


Source Code

This filter is located in form_display.php

Search the Documentation