How to pre-fill clone fields

Created by Phil Kurth, Modified on Fri, 01 Jul 2022 at 09:29 AM by Phil Kurth

Pre-filling clone fields can be a bit confusing as the name and key of the field will change depending on how the clone field is configured. Specifically, if the Prefix Field Names setting is enabled, the clone field's name/key must be joined with the name/key of the field being cloned in order to target it when pre-filling. 



The following example demonstrates how to work with the clone field in both config scenarios when using field names. 


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

  $clone = 'YOUR_CLONE_FIELD_NAME_HERE';
  $cloned = 'YOUR_CLONED_FIELD_NAME_HERE';

  // If the Prefix Field Names setting is ENABLED, the field names need to be aggregated.
  if ( $field['name'] === "{$clone}_{$cloned}" ) {
    $value = 'Value 1';
  }

  // If the Prefix Field Names setting is DISABLED, just use the field name of the field being cloned.
  if ( $field['name'] === $cloned ) {
    $value = 'Value 2';
  }

  return $value;
}, 10, 4 );


If you are using field keys, the approach is the same, just replace the field names with the keys. e.g; a prefixed field key will look something like this: field_123456_field_234567.

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 atleast one of the reasons

Feedback sent

We appreciate your effort and will try to fix the article