gform_save_field_value

Description

Use this filter to change the field’s value before saving it to the database. Use in conjunction with gform_get_input_value to perform low level transformations, such as encrypting/decrypting a field.

Usage

The base filter which would run for all forms and all fields would be used like so:

add_filter( 'gform_save_field_value', 'your_function_name', 10, 5 );

To target a specific form append the form id to the hook name. (format: gform_save_field_value_FORMID)

add_filter( 'gform_save_field_value_10', 'your_function_name', 10, 5 );

To target a specific field append both the form id and the field id to the hook name. (format: gform_save_field_valuee_FORMID_FIELDID)

add_filter( 'gform_save_field_value_10_3', 'your_function_name', 10, 5 );

Parameters

  • $value string

    The current entry value to be filtered.

  • $entry Entry Object

    The current entry.

  • $field Field Object | null

    The field from which the entry value was submitted or null if updating the entry and the field no longer exists.

  • $form Form Object

    The form from which the entry value was submitted.

  • $input_id Mixed

    The input ID of the input being saved. Defaults to the field ID for single input field types. Added in GF v1.8.5.8

Examples

1. Encode all values

This example base 64 encodes the field values. View gform_get_input_value for an example on how to decode the fields.

add_filter( 'gform_save_field_value', 'save_field_value', 10, 4 );
function save_field_value( $value, $lead, $field, $form ) {
    return base64_encode( $value );
}

This example uses the GFCommon::encrypt() method.

add_filter( 'gform_save_field_value', 'save_field_value', 10, 4 );
function save_field_value( $value, $lead, $field, $form ) {
    return GFCommon::encrypt( $value );
}

2. Encode values for a specific form

This is another example where you may select a specific form and specific fields to encode.

add_filter( 'gform_save_field_value', 'save_field_value', 10, 4 );
function save_field_value( $value, $lead, $field, $form ) {
        //if not the form with fields to encode, just return the unaltered value without checking the fields
        if ( ! is_object( $field ) || absint( $form->id ) <> 94 ) {
                return $value;
        }
 
        //array of field ids to encode
        $encode_fields = array( 1, 2, 3 );
 
        //see if the current field id is in the array of fields to encode; encode if so, otherwise return unaltered value
        if ( in_array( $field->id, $encode_fields ) ) {
                return base64_encode( $value );
        } else {
                return $value;
        }
}

3. Process merge tags

The following example shows how you can replace merge tags before saving the field value.

add_filter( 'gform_save_field_value', 'replace_merge_tags', 10, 4 );
function replace_merge_tags( $value, $entry, $field, $form ) {
	$value = GFCommon::replace_variables( $value, $form, $entry );
	return $value;
}

4. Uppercase Value

The following example shows how you can uppercase a field value when the entry is saved.

add_filter( 'gform_save_field_value', 'uppercase_text', 10, 3 );
function uppercase_text( $value, $entry, $field ) {
    if ( $field->get_input_type() == 'text' ) {
        $value = strtoupper( $value );
    }
 
    return $value;
}

5. Remove new lines, tabs and carriage returns

The following example shows how you can remove new lines, tabs and carriage returns before saving the field value.

add_filter("gform_save_field_value", "remove_n_t_r", 10, 4);
function remove_n_t_r($value, $entry, $field, $form){
	$new_value = str_replace(array("\n", "\t", "\r"), '', $value);
	return $new_value;
}

6. Return the current date in your desired format

The following example will return current local date in Y-m-d format. Use PHP’s date format to customize date format to your like.

	add_filter("gform_save_field_value", "current_date", 10, 4);
	function current_date( $value, $entry, $field, $form ){
		$local_timestamp = GFCommon::get_local_timestamp( time() );
		$local_date      = date_i18n( 'Y-m-d', $local_timestamp, true );

		return $local_date;
	}

7. Skip saving of Credit Card type and last four digits

To be compliant with PCI standards, only the card type and last four digits are stored. The following example can be used to not store this information either.

add_filter( 'gform_save_field_value', 'gf_empty_card_field', 10, 4 );
function gf_empty_card_field( $value, $lead, $field, $form ) {
    if ( $field->type === 'creditcard' || $field->type === 'stripe_creditcard' ){
        $value = false;
    }
    return $value;
}

8. Make a value’s first character uppercase

The following example shows how you can uppercase only the first character of a field value when the entry is saved. This time we’re limiting the snippet scope by adding the form id (540) and field id (3) to the filter name.

// Change 540 to your form id number, and 3 to your field id.
add_filter( 'gform_save_field_value_540_3', 'gf_uppercase_first_character', 10, 4 );
function gf_uppercase_first_character( $value, $lead, $field, $form ) {
	$value = ucfirst( $value );
	GFCommon::log_debug( __METHOD__ . '(): Modified value => ' . $value );
	return $value;
}

9. Change value for all inputs of an Address field except for State

The following example shows how to change all values of an Address field except State.

add_action( 'gform_save_field_value', function( $value, $lead, $field, $form, $input_id ) {
	$state_input = $field->id . '.4';
	// Apply change for all inputs of Address fields except State.
	if ( $field->type === 'address' && $state_input != $input_id ){
		$value = strtoupper( $value );
		GFCommon::log_debug( __METHOD__ . "(): New value for {$input_id}: {$value}" );
	}
	
	return $value;
}, 10, 5 );

Placement

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

Source Code

gf_apply_filters( 'gform_save_field_value', array( $form['id'], $field->id ), $value, $lead, $field, $form, $input_id )

This filter is located in the following methods in forms_model.php

  • GFFormsModel::get_prepared_input_value()
  • GFFormsModel::update_lead_field_value()

Since

The base filter was added in Gravity Forms 1.5. The form and field specific versions were added in Gravity Forms 1.9.13.6