gform_user_registration_user_data_pre_populate

Description

This filter can be used 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.

Examples

1. Override a field value

The following example shows how you can override the value for field 12 in form id 3.

This example can also be used to prevent the mapped field from being populated with the saved value by returning an empty value. This would allow you to use the Default Value setting in the field.

add_filter( 'gform_user_registration_user_data_pre_populate', function ( $mapped_fields, $form, $feed ) {
    gf_user_registration()->log_debug( 'Running for form id ' . $form['id'] );
    // Run only for form 3.
    if ( $form['id'] != '3' ) {
        return $mapped_fields;
    }
    // Replace "12" with the field ID you want to display empty.
    $mapped_fields[12] = 'My Custom Value';

    return $mapped_fields;
}, 10, 3 );

2. Log the values

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

3. Replace country code

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

4. Unserialize data

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

function gf_unserialize_data( $mapped_fields, $form, $feed ) {
    // Replace "12" with the field ID you want to replace.
    $mapped_fields[12] = maybe_unserialize( $mapped_fields[12] );

    return $mapped_fields;
}

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?

Since

This filter was added in version 2.3.

Source Code

$mapped_fields = apply_filters( 'gform_user_registration_user_data_pre_populate', $mapped_fields, $form, $feed );

This filter is located in GF_User_Registration::prepopulate_form() in class-gf-user-registration.php.