How to pre-fill group fields

Created by Phil Kurth, Modified on Fri, 1 Jul, 2022 at 9:32 AM by Phil Kurth

Group fields require data in a certain format in order to render correctly. The required format is an array of sub field keys mapped to the pre-filled values. e.g; 

add_filter( 'af/field/prefill_value', function ( $value, $field, $form, $args ) {
  // If we need to run this only on a specific form, check for the form key.
  if ( $form['key'] !== 'YOUR_FORM_KEY_HERE' ) {
    return $value;
  }
  
  // Check for the desired group field and override the value by mapping sub field values to keys.
  if ( $field['name'] === 'YOUR_GROUP_FIELD_NAME_HERE' ) {
    $value = [
      'field_12345678' => 'Sub field 1 value',
      'field_23456789' => 'Sub field 2 value',
    ];
  }
  
  return $value;
}, 10, 4 );

Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article