Using Gravity Forms ‘gform_user_registration_prepared_value’ PHP filter

The gform_user_registration_prepared_value Gravity Forms PHP filter allows you to modify a field value before it is saved to the user meta.

Usage

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

Parameters

  • $value (string) – The value to be modified.
  • $field (Field Object) – The field currently being processed.
  • $input_id (string) – The ID of the field input currently being processed.
  • $entry (Entry Object) – The entry currently being processed.
  • $is_username (bool) – Indicates if the current field is mapped to the username.

More information

See Gravity Forms Docs: gform_user_registration_prepared_value

Examples

Country Code

Replace the address field country value with the country code:

add_filter('gform_user_registration_prepared_value', function($value, $field, $input_id, $entry, $is_username) {
    $country_id = $field->id . '.6';
    if ($field->type == 'address' && $input_id == $country_id) {
        return GF_Fields::get('address')->get_country_code($value);
    }
    return $value;
}, 10, 5);

US State Code

Replace the address field US state value with the state code:

add_filter('gform_user_registration_prepared_value', function($value, $field, $input_id, $entry, $is_username) {
    $state_id = $field->id . '.4';
    if ($field->type == 'address' && $input_id == $state_id) {
        return GF_Fields::get('address')->get_us_state_code($value);
    }
    return $value;
}, 10, 5);

Checkboxes as serialized data

Serialize values of a checkboxes field before saving to the user meta for a field with id 18:

add_filter('gform_user_registration_prepared_value', 'gf_serialize_checkboxes', 10, 5);

function gf_serialize_checkboxes($value, $field, $input_id, $entry, $is_username) {
    if ($field->id == '18' && $field->type == 'checkbox') { // Update 18 to your field id number
        // Get a comma separated list of checkboxes checked
        $checked = $field->get_value_export($entry);

        // Convert to array
        $value = explode(', ', $checked);

        // WordPress will automatically serialize this array before saving.
    }
    return $value;
}

Uppercase user meta value

Convert a custom user meta field value to uppercase before saving:

add_filter('gform_user_registration_prepared_value', function($value, $field, $input_id, $entry, $is_username) {
    if ($field->id == '12' && $field->type == 'text') { // Update 12 to your field id number
        return strtoupper($value);
    }
    return $value;
}, 10, 5);

Save only digits from a phone field

Save only digits from a phone field with id 9 to user meta:

add_filter('gform_user_registration_prepared_value', function($value, $field, $input_id, $entry, $is_username) {
    if ($field->id == '9' && $field->type == 'phone') { // Update 9