DescriptionUsageParametersExamples1. Override a field value2. Log the values3. Replace country code4. Unserialize dataPlacementSinceSource Code
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.
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 );
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
Your code snippet should be placed in the functions.php file of your active theme.
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.