Using Gravity Forms ‘gform_add_field_buttons’ PHP filter

The gform_add_field_buttons filter allows you to add, edit, or remove the “add field” buttons from the form editor’s floating toolbox in Gravity Forms.

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 has a “fields” array containing all the fields in the group.

More information

See Gravity Forms Docs: gform_add_field_buttons

Examples

Add a “Map” field button to the Advanced group

This example demonstrates how to add a “Map” field button to the Advanced group.

function add_map_field($field_groups) {
    // Define the new Map field button
    $map_field_button = array(
        'class' => 'button',
        'data-type' => 'map',
        'value' => __('Map', 'gravityforms')
    );

    // Find the Advanced Fields group
    foreach ($field_groups as &$group) {
        if ($group['name'] == 'advanced_fields') {
            // Add the new Map field button to the Advanced Fields group
            $group['fields'][] = $map_field_button;
            break;
        }
    }

    return $field_groups;
}

add_filter('gform_add_field_buttons', 'add_map_field');

Remove the “Website” field button from the Advanced group

This example shows how to remove the “Website” field button from the Advanced group.

function remove_website_field($field_groups) {
    // Iterate through the field groups
    foreach ($field_groups as &$group) {
        if ($group['name'] == 'advanced_fields') {
            // Iterate through the fields in the Advanced Fields group
            foreach ($group['fields'] as $key => $field) {
                if ($field['data-type'] == 'website') {
                    // Remove the Website field button
                    unset($group['fields'][$key]);
                    break;
                }
            }
        }
    }

    return $field_groups;
}

add_filter('gform_add_field_buttons', 'remove_website_field');

Add a “Custom Field” button to the Standard group

This example explains how to add a “Custom Field” button to the Standard group.

function add_custom_field_button($field_groups) {
    // Define the new Custom Field button
    $custom_field_button = array(
        'class' => 'button',
        'data-type' => 'custom',
        'value' => __('Custom Field', 'gravityforms')
    );

    // Find the Standard Fields group
    foreach ($field_groups as &$group) {
        if ($group['name'] == 'standard_fields') {
            // Add the new Custom Field button to the Standard Fields group
            $group['fields'][] = $custom_field_button;
            break;
        }
    }

    return $field_groups;
}

add_filter('gform_add_field_buttons', 'add_custom_field_button');

Change the label of the “Email” field button in the Advanced group

This example demonstrates how to change the label of the “Email” field button in the Advanced group.

function change_email_field_label($field_groups) {
    // Iterate through the field groups
    foreach ($field_groups as &$group) {
        if ($group['name'] == 'advanced_fields') {
            // Iterate through the fields in the Advanced Fields group
foreach ($group['fields'] as &$field) {
if ($field['data-type'] == 'email') {
// Change the label of the Email field button
$field['value'] = __('Custom Email', 'gravityforms');
break;
}
}
}
}

return $field_groups;
}

add_filter('gform_add_field_buttons', 'change_email_field_label');

Reorder field buttons in the Standard Fields group

This example shows how to reorder field buttons in the Standard Fields group.

add_filter(‘gform_add_field_buttons’, ‘change_email_field_label’);

function reorder_standard_field_buttons($field_groups) {
    // Iterate through the field groups
    foreach ($field_groups as &$group) {
        if ($group['name'] == 'standard_fields') {
            // Reorder field buttons by their 'value' attribute
            usort($group['fields'], function($a, $b) {
                return strcmp($a['value'], $b['value']);
            });
            break;
        }
    }

    return $field_groups;
}

add_filter('gform_add_field_buttons', 'reorder_standard_field_buttons');