Using Gravity Forms ‘gform_save_field_value’ PHP filter

The gform_save_field_value Gravity Forms PHP action allows you to modify a field’s value before it is saved to the database. It can be used in conjunction with gform_get_input_value to perform low-level transformations, such as encrypting/decrypting a field.

Usage

A generic example of how to use the action:

add_filter('gform_save_field_value', '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

More information

See Gravity Forms Docs: gform_save_field_value

Examples

Encode all values

This example base64 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);
}

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

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

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