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

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:

$standard_fields = array(
    array( 'class' => 'button', 'data-type' => 'text', 'value' => GFCommon::get_field_type_title( 'text' ) ),
    array( 'class' => 'button', 'data-type' => 'textarea', 'value' => GFCommon::get_field_type_title( 'textarea' ) ),
    array( 'class' => 'button', 'data-type' => 'select', 'value' => GFCommon::get_field_type_title( 'select' ) ),
    array( 'class' => 'button', 'data-type' => 'multiselect', 'value' => GFCommon::get_field_type_title( 'multiselect' ) ),
    array( 'class' => 'button', 'data-type' => 'number', 'value' => GFCommon::get_field_type_title( 'number' ) ),
    array( 'class' => 'button', 'data-type' => 'checkbox', 'value' => GFCommon::get_field_type_title( 'checkbox' ) ),
    array( 'class' => 'button', 'data-type' => 'radio', 'value' => GFCommon::get_field_type_title( 'radio' ) ),
    array( 'class' => 'button', 'data-type' => 'hidden', 'value' => GFCommon::get_field_type_title( 'hidden' ) ),
    array( 'class' => 'button', 'data-type' => 'html', 'value' => GFCommon::get_field_type_title( 'html' ) ),
    array( 'class' => 'button', 'data-type' => 'section', 'value' => GFCommon::get_field_type_title( 'section' ) ),
    array( 'class' => 'button', 'data-type' => 'page', 'value' => GFCommon::get_field_type_title( 'page' ) ),
);

$advanced_fields = array(
    array( 'class' => 'button', 'data-type' => 'name', 'value' => GFCommon::get_field_type_title( 'name' ) ),
    array( 'class' => 'button', 'data-type' => 'date', 'value' => GFCommon::get_field_type_title( 'date' ) ),
    array( 'class' => 'button', 'data-type' => 'time', 'value' => GFCommon::get_field_type_title( 'time' ) ),
    array( 'class' => 'button', 'data-type' => 'phone', 'value' => GFCommon::get_field_type_title( 'phone' ) ),
    array( 'class' => 'button', 'data-type' => 'address', 'value' => GFCommon::get_field_type_title( 'address' ) ),
    array( 'class' => 'button', 'data-type' => 'website', 'value' => GFCommon::get_field_type_title( 'website' ) ),
    array( 'class' => 'button', 'data-type' => 'email', 'value' => GFCommon::get_field_type_title( 'email' ) ),
    array( 'class' => 'button', 'data-type' => 'password', 'value' => GFCommon::get_field_type_title( 'password' ) ),
    array( 'class' => 'button', 'data-type' => 'fileupload', 'value' => GFCommon::get_field_type_title( 'fileupload' ) ),
    array( 'class' => 'button', 'data-type' => 'captcha', 'value' => GFCommon::get_field_type_title( 'captcha' ) ),
    array( 'class' => 'button', 'data-type' => 'list', 'value' => GFCommon::get_field_type_title( 'list' ) )
);

$post_fields = array(
    array( 'class' => 'button', 'data-type' => 'post_title', 'value' => GFCommon::get_field_type_title( 'post_title' ) ),
    array( 'class' => 'button', 'data-type' => 'post_content', 'value' => GFCommon::get_field_type_title( 'post_content' ) ),
    array( 'class' => 'button', 'data-type' => 'post_excerpt', 'value' => GFCommon::get_field_type_title( 'post_excerpt' ) ),
    array( 'class' => 'button', 'data-type' => 'post_tags', 'value' => GFCommon::get_field_type_title( 'post_tags' ) ),
    array( 'class' => 'button', 'data-type' => 'post_category', 'value' => GFCommon::get_field_type_title( 'post_category' ) ),
    array( 'class' => 'button', 'data-type' => 'post_image', 'value' => GFCommon::get_field_type_title( 'post_image' ) ),
    array( 'class' => 'button', 'data-type' => 'post_custom_field', 'value' => GFCommon::get_field_type_title( 'post_custom_field' ) ),
);

$pricing_fields = array(
    array( 'class' => 'button', 'data-type' => 'product', 'value' => GFCommon::get_field_type_title( 'product' ) ),
    array( 'class' => 'button', 'data-type' => 'quantity', 'value' => GFCommon::get_field_type_title( 'quantity' ) ),
    array( 'class' => 'button', 'data-type' => 'option', 'value' => GFCommon::get_field_type_title( 'option' ) ),
    array( 'class' => 'button', 'data-type' => 'shipping', 'value' => GFCommon::get_field_type_title( 'shipping' ) ),
    array( 'class' => 'button', 'data-type' => 'total', 'value' => GFCommon::get_field_type_title( 'total' ) ),
    array( 'class' => 'button', 'data-type' => 'creditcard', 'value' => GFCommon::get_field_type_title( 'creditcard' ) ),
);

$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 ),
    array( 'name' => 'pricing_fields', 'label' => __( 'Pricing Fields', 'gravityforms' ), 'fields' => $pricing_fields )
);

Examples

Example One

This example adds a “Map” field button to the advanced group

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',
                'data-type' => 'map',
                'value'     => __( 'Map', 'gravityforms' ),
                'onclick'   => "StartAddField('map');"
            );
            break;
        }
    }

    return $field_groups;
}

Example Two

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

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;
        } elseif ( $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;
}

Example Three

This example updates the onclick attribute of the coupon field.

add_filter( 'gform_add_field_buttons', function ( $field_groups ) {
    foreach ( $field_groups as &$group ) {
        if ( $group['name'] == 'pricing_fields' ) {
            foreach ( $group['fields'] as &$field ) {
                if ( isset( $field['data-type'] ) && $field['data-type'] == 'coupon' ) {
                    $field['onclick'] = "StartAddCouponField('coupon');";
                    break;
                }
            }
            break;
        }
    }

    return $field_groups;
} );

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

This filter is located in form_detail.php