bookmark_bordergform_field_value_$parameter_name

Description

This filter is executed before displaying each field and can be used to dynamically populate fields with a default value.

This filter can also be used to pre-select or pre-check drop down, radio button and checkbox items.

Note: This filter requires that the “Allow field to be populated dynamically” option is checked in the field editor’s advanced tab.

Usage

add_filter( 'gform_field_value_email', 'your_function_name' );

You can also use the filter without including a parameter name e.g.

add_filter( 'gform_field_value', 'your_function_name' );

Parameters

  • $value string

    The string containing the current field value to be filtered.

  • $field Field Object

    The current field being processed.

  • $name string

    The parameter name of the field or input being processed.

Examples

1. Populate a fixed date

The following example populates the date field with a hard-coded value. It assumes that the date field is configured to “Allow field to be populated dynamically” and that the parameter name is set to “date”

add_filter( 'gform_field_value_date', 'populate_date' );
function populate_date( $value ) {
   return '10/10/2010';
}

2. Populate from a cookie

The following example populates a hidden field with content from a cookie. It assumes that the hidden field is configured to “Allow field to be populated dynamically” and that the parameter name is set to “utm_campaign” and the cookie has the same name.

add_filter( 'gform_field_value_utm_campaign', 'populate_utm_campaign' );
function populate_utm_campaign( $value ) {
   return $_COOKIE['utm_campaign'];
}

3. List Field

The following examples populate a 3 column list field with some values. It assumes that the list field is configured to “Allow field to be populated dynamically” and that the parameter name is set to “list”. The second example accepts a new array format that is available as of Gravity Forms 1.9.10.8. This format is the same format in which the data is saved in the database and much more user friendly. Example one is left for backwards compatibility since this format is still accepted.

Old Array Format

add_filter( 'gform_field_value_list', 'populate_list' );
function populate_list( $value ) {
   return array( 'row 1 - col1', 'row 1 - col2', 'row 1 - col3',
                'row 2 - col1', 'row 2 - col2', 'row 2 - col3',
                'row 3 - col1', 'row 3 - col2', 'row 3 - col3' );

}

New Array Format

add_filter( 'gform_field_value_list', 'populate_list' );
function populate_list( $value ) {
  $list_array = array(
	  		array(
				'Column 1' => 'row1col1',
				'Column 2' => 'row1col2',
				'Column 3' => 'row1col3',
			),
	  		array(
				'Column 1' => 'row2col1',
 				'Column 2' => 'row2col2',
 				'Column 3' => 'row2col3'
			),
  );
	return $list_array;
}

4. Populate multiple fields using one function

add_filter( 'gform_field_value', 'populate_fields', 10, 3 );
function populate_fields( $value, $field, $name ) {

    $values = array(
        'field_one'   => 'value one',
        'field_two'   => 'value two',
        'field_three' => 'value three',
    );

    return isset( $values[ $name ] ) ? $values[ $name ] : $value;
}

5. Populate the time

The following example populates the time field with the current time. It assumes that the date field is configured to “Allow field to be populated dynamically” and that the parameter name is set to “time”

add_filter( 'gform_field_value_time', 'populate_time' );
function populate_time( $value ) {
	$local_timestamp = GFCommon::get_local_timestamp( time() );

	return date_i18n( 'h:i A', $local_timestamp, true );
}

Placement

This code should be placed in the functions.php file of your active theme.

Source Code

This filter is located in GFFormsModel::get_parameter_value() in forms_model.php