gform_field_input

Description

The gform_field_input filter is executed before creating the field’s input tag, allowing users to modify the field’s input tag. It can also be used to create custom field types.

Usage

Apply to all forms.

add_filter( 'gform_field_input', 'my_custom_function', 10, 5 );

Apply to a specific form.

add_filter( 'gform_field_input_123', 'my_custom_function', 10, 5 ); // Apply to Form ID 123

Apply to a specific form and field.

add_filter( 'gform_field_input_123_6', 'my_custom_function', 10, 5 ); // Apply to Form ID 123 and Field ID 6

Parameters

ParameterTypeDescription
$inputstringThe input tag string to be filtered. Will be passed to the hook an empty string value. Return an empty string to bypass the filtering, or change its value to specify a new input tag.
$fieldField ObjectThe field that this input tag applies to.
$valuestringThe default/initial value that the field should be pre-populated with.
$entry_idintegerWhen executed from the entry detail screen, $entry_id will be populated with the Entry ID. Otherwise, it will be 0.
$form_idintegerThe current Form ID.

Examples

Create a Google Maps field.

This example creates a Google map field, replacing fields with a google_map custom CSS class. This example is not intended to be fully functional, but to demonstrate what can be accomplished with this hook. In combination with some other Administration hooks, this hook can be used to create completely custom field types, such as a Map field type.

add_filter( 'gform_field_input', 'map_input', 10, 5 );
function map_input( $input, $field, $value, $lead_id, $form_id ) {
    if ( $field->cssClass == 'google_map' ) {
        $input = '<div class="ginput_container"><iframe width="300" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/?ie=UTF8&ll=37.09024,-95.712891&spn=20.981197,26.367188&z=4&output=embed"></iframe><br /><small><a href="http://maps.google.com/?ie=UTF8&ll=37.09024,-95.712891&spn=20.981197,26.367188&z=4&source=embed" style="color:#0000FF;text-align:left">View Larger Map</a></small></div>';
    }
    return $input;
}

Create a Hidden field.

This example creates a hidden field on the form using a specific name.

Note: Using a custom name attribute, then Gravity Forms won’t know how to access the value, so it will not be saved during form submission.

add_filter( 'gform_field_input', 'map_input', 10, 5 );
function map_input( $input, $field, $value, $lead_id, $form_id ) {
    if ( $field->cssClass == 'google_map' ) {
        $input = '<div class="ginput_container"><iframe width="300" height="300" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com/?ie=UTF8&amp;ll=37.09024,-95.712891&amp;spn=20.981197,26.367188&amp;z=4&amp;output=embed"></iframe><br /><small><a href="http://maps.google.com/?ie=UTF8&amp;ll=37.09024,-95.712891&amp;spn=20.981197,26.367188&amp;z=4&amp;source=embed" style="color:#0000FF;text-align:left">View Larger Map</a></small></div>';
    }
    return $input;
}

Display an image from a post on the form.

This example displays an image from a post on the form by replacing the <input> tag with the <img> tag returned from WordPress’s wp_get_attachment_image function. The attachment id must be known.

add_filter( 'gform_field_input', 'display_attachment', 10, 5 );
function display_attachment( $input, $field, $value, $lead_id, $form_id ) {
    //because this will fire for every form/field, only do it when it is the specific form and field
    if ( $form_id == 23 && $field->id == 12 ) {
        $input = wp_get_attachment_image( 114 );
    }
    return $input;
}

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 GFCommon::get_field_input() in common.php