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:

		$field_groups = array(
			'standard_fields' => array(
				'name'          => 'standard_fields',
				'label'         => __( 'Standard Fields', 'gravityforms' ),
				'tooltip_class' => 'tooltip_bottomleft',
				'fields' => array(),
				'fields'        => array(
					array( 'data-type' => 'text',        'value' => GFCommon::get_field_type_title( 'text' ) ),
					array( 'data-type' => 'textarea',    'value' => GFCommon::get_field_type_title( 'textarea' ) ),
					array( 'data-type' => 'select',      'value' => GFCommon::get_field_type_title( 'select' ) ),
					array( 'data-type' => 'number',      'value' => GFCommon::get_field_type_title( 'number' ) ),
					array( 'data-type' => 'checkbox',    'value' => GFCommon::get_field_type_title( 'checkbox' ) ),
					array( 'data-type' => 'radio',       'value' => GFCommon::get_field_type_title( 'radio' ) ),
					array( 'data-type' => 'hidden',      'value' => GFCommon::get_field_type_title( 'hidden' ) ),
					array( 'data-type' => 'html',        'value' => GFCommon::get_field_type_title( 'html' ) ),
					array( 'data-type' => 'section',     'value' => GFCommon::get_field_type_title( 'section' ) ),
					array( 'data-type' => 'page',        'value' => GFCommon::get_field_type_title( 'page' ) ),
				),
			),
			'advanced_fields' => array(
				'name'   => 'advanced_fields',
				'label'  => __( 'Advanced Fields', 'gravityforms' ),
				'fields' => array(
					array( 'data-type' => 'name',        'value' => GFCommon::get_field_type_title( 'name' ) ),
					array( 'data-type' => 'date',        'value' => GFCommon::get_field_type_title( 'date' ) ),
					array( 'data-type' => 'time',        'value' => GFCommon::get_field_type_title( 'time' ) ),
					array( 'data-type' => 'phone',       'value' => GFCommon::get_field_type_title( 'phone' ) ),
					array( 'data-type' => 'address',     'value' => GFCommon::get_field_type_title( 'address' ) ),
					array( 'data-type' => 'website',     'value' => GFCommon::get_field_type_title( 'website' ) ),
					array( 'data-type' => 'email',       'value' => GFCommon::get_field_type_title( 'email' ) ),
					array( 'data-type' => 'fileupload',  'value' => GFCommon::get_field_type_title( 'fileupload' ) ),
					array( 'data-type' => 'captcha',     'value' => GFCommon::get_field_type_title( 'captcha' ) ),
					array( 'data-type' => 'list',        'value' => GFCommon::get_field_type_title( 'list' ) ),
					array( 'data-type' => 'multiselect', 'value' => GFCommon::get_field_type_title( 'multiselect' ) ),
				),
			),
			'post_fields'     => array(
				'name'   => 'post_fields',
				'label'  => __( 'Post Fields', 'gravityforms' ),
				'fields' => array(
					array( 'data-type' => 'post_title',        'value' => GFCommon::get_field_type_title( 'post_title' ) ),
					array( 'data-type' => 'post_content',      'value' => GFCommon::get_field_type_title( 'post_content' ) ),
					array( 'data-type' => 'post_excerpt',      'value' => GFCommon::get_field_type_title( 'post_excerpt' ) ),
					array( 'data-type' => 'post_tags',         'value' => GFCommon::get_field_type_title( 'post_tags' ) ),
					array( 'data-type' => 'post_category',     'value' => GFCommon::get_field_type_title( 'post_category' ) ),
					array( 'data-type' => 'post_image',        'value' => GFCommon::get_field_type_title( 'post_image' ) ),
					array( 'data-type' => 'post_custom_field', 'value' => GFCommon::get_field_type_title( 'post_custom_field' ) ),
				),
			),
			'pricing_fields'   => array(
				'name'   => 'pricing_fields',
				'label'  => __( 'Pricing Fields', 'gravityforms' ),
				'fields' => array(
					array( 'data-type' => 'product',  'value' => GFCommon::get_field_type_title( 'product' ) ),
					array( 'data-type' => 'quantity', 'value' => GFCommon::get_field_type_title( 'quantity' ) ),
					array( 'data-type' => 'option',   'value' => GFCommon::get_field_type_title( 'option' ) ),
					array( 'data-type' => 'shipping', 'value' => GFCommon::get_field_type_title( 'shipping' ) ),
					array( 'data-type' => 'total',    'value' => GFCommon::get_field_type_title( 'total' ) ),
				),
			),
		);

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 can be used in the functions.php file of the active theme, a custom functions plugin, a custom add-on, or with a code snippets plugin.

See also the PHP section in this article: Where Do I Put This Code?

Source Code

This filter is located in form_detail.php