gform_field_input

Description

This 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 a specific form and field
add_filter( 'gform_field_input_123_6', 'my_custom_function', 10, 5 );

Parameters

  • $input string

    The 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.

  • $field Field Object

    The field that this input tag applies to

  • $value string

    The default/initial value that the field should be pre-populated with

  • $entry_id integer

    When executed from the entry detail screen, $lead_id will be populated with the Entry ID. Otherwise, it will be 0

  • $form_id integer

    The current Form ID.

Examples

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;
}

This example creates a hidden field on the form using a specific name. Please note: if you use 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', 'tracker', 10, 5 );
function tracker( $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 == 11 ) {
        $input = '<input type="hidden" id="hidTracker" name="hidTracker" value="test">';
    }
    return $input;
}

This example displays an image from a post on the form by replacing the <input> tag with the <img> tag returned from WordPress’ wp_get_attachment_image function. You need to know the attachment id.

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 should be placed in the functions.php file of your active theme.

Source Code

This filter is located in GFCommon::get_field_input() in common.php