Using Gravity Forms ‘gform_user_registration_user_data_pre_populate’ PHP filter

The gform_user_registration_user_data_pre_populate filter allows you to modify a value before it is populated into the field.

Usage

add_filter( 'gform_user_registration_user_data_pre_populate', 'your_function_name', 10, 3 );

Parameters

  • $mapped_fields (array): An array with the field ID as the key to the value to be populated into that field.
  • $form (Form Object): The form currently being processed.
  • $feed (Feed Object): The feed currently being processed.

More information

See Gravity Forms Docs: gform_user_registration_user_data_pre_populate

Examples

Override a field value

Override the value for field 12.

add_filter( 'gform_user_registration_user_data_pre_populate', function( $mapped_fields, $form, $feed ) {
    // replace "12" with the field ID you want to replace
    $mapped_fields[12] = 'My Custom Value';

    return $mapped_fields;
}, 10, 3 );

Log the values

Log the values of mapped fields.

add_filter( 'gform_user_registration_user_data_pre_populate', function( $mapped_fields, $form, $feed ) {
    gf_user_registration()->log_debug( 'gform_user_registration_user_data_pre_populate: $mapped_fields => ' . print_r( $mapped_fields, 1 ) );

    return $mapped_fields;
}, 10, 3 );

Replace country code

Replace country code with the corresponding country name.

add_filter( 'gform_user_registration_user_data_pre_populate', function( $mapped_fields, $form, $feed ) {
    $fields = GFCommon::get_fields_by_type( $form, array( 'address' ) );

    if ( ! empty( $fields ) ) {
        $codes = GF_Fields::get( 'address' )->get_country_codes();
        foreach ( $fields as $field ) {
            $country_id = $field->id . '.6';
            $value      = rgar( $mapped_fields, $country_id );
            if ( ! empty( $value ) ) {
                $mapped_fields[ $country_id ] = array_search( $value, $codes );
            }
        }
    }

    return $mapped_fields;
}, 10, 3 );

Unserialize data

Unserialize data before populating it into the field.

add_filter( 'gform_user_registration_user_data_pre_populate', function( $mapped_fields, $form, $feed ) {
    foreach ( $mapped_fields as $key => $value ) {
        if ( is_serialized( $value ) ) {
            $mapped_fields[ $key ] = unserialize( $value );
        }
    }

    return $mapped_fields;
}, 10, 3 );

Remove empty values

Remove empty values from the mapped fields.

add_filter( 'gform_user_registration_user_data_pre_populate', function( $mapped_fields, $form, $feed ) {
    $mapped_fields = array_filter( $mapped_fields, function( $value ) {
        return ! empty( $value );
    });

    return $mapped_fields;
}, 10, 3 );