Documentation

gform_field_input

  1. Description
  2. Usage
  3. Parameters
  4. Examples
  5. Placement
  6. Source Code


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

<?php
add_filter( 'gform_field_input', 'map_input', 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.

<?php
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.

<?php
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.

<?php
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 common.php