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. When pre-selecting or pre-checking choice-based fields, the value returned by your function must match the value of the label exactly or, if you are using the option to ‘Show Values‘, the value returned by the function must match the value exactly.
Please note: This filter requires that the “Allow field to be populated dynamically” option is checked in the field editor’s advanced tab. Also: avoid using a parameter name from this list of reserved WordPress terms.
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
Pre-select a radio button, checkbox or dropdown option
The following example pre-selects a single choice in a radio button, checkbox, or dropdown field that is configured to “Allow field to be populated dynamically” and where the parameter name is set to “pet”.
add_filter( 'gform_field_value_pet', 'pre_select_pet' );
function pre_select_pet( $value ) {
return 'Dog';
}
Pre-check multiple checkboxes
The following example pre-checks two checkboxes in a checkbox field that is configured to “Allow field to be populated dynamically” and where the parameter name is set to “myterms”. Note: terms is a WordPress reserved term which is why myterms was used instead.
add_filter( 'gform_field_value_myterms', 'pre_check_terms' );
function pre_check_terms( $value ) {
return 'Books,Movies';
}
This method can also be used to pre-select multiple options in a multi-select field.
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';
}
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 ) {
GFCommon::log_debug( 'Value from cookie => ' . $_COOKIE['utm_campaign'] );
return $_COOKIE['utm_campaign'];
}
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;
}
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;
}
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 );
}
Populate current date
The following example populates a date field with the current date. It assumes that the date field is configured to “Allow field to be populated dynamically” and that the parameter name is set to “current_date”
add_filter( 'gform_field_value_current_date', 'populate_current_date' );
function populate_current_date( $value ) {
$local_timestamp = GFCommon::get_local_timestamp( time() );
return date_i18n( 'm/d/Y', $local_timestamp, true );
}
Populate previous value
The following example shows how a field can be populated with the previous value submitted for the current form and field by the logged in user. In this case, the dynamic population parameter name setting on the field is set to “previous_value”.
add_filter( 'gform_field_value_previous_value', 'populate_previous_value', 10, 2 );
function populate_previous_value( $value, $field ) {
$entries = GFAPI::get_entries(
$field->formId,
array(
'field_filters' => array(
array(
'key' => 'created_by',
'value' => get_current_user_id(),
),
),
),
array(),
array( 'page_size' => 1 )
);
return rgars( $entries, '0/' . $field->id );
}
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 GFFormsModel::get_parameter_value() in forms_model.php