How to pre-fill form field values

Created by Phil Kurth, Modified on Fri, 24 Jun 2022 at 09:39 AM by Phil Kurth

When using the advanced_form() function to render a form, you may use the `values` form arg to pre-fill field values. e.g; 

advanced_form( 'FORM_KEY', [
  'values' => [
    'first_name' => 'John',
    'last_name' => 'Doe',
  ],
] );

Note that the `values` form arg will only work when rendering a form using the function — the [advanced_form ...] shortcode does not support the use of the `values` argument. 


You may also choose to programmatically pre-fill values using the af/field/prefill_value filter.

function prefill_field_value_example( $value, $field, $form, $args ) {
  
  // If we aren't handling the desired form, return early.
  if ( $form['key'] !== 'FORM_KEY_HERE' ) {
    return $value;
  }

  // Set the value for desired fields.
  if ( $field['name'] === 'first_name' ) {
    return 'John';
  }

  if ( $field['name'] === 'last_name' ) {
    return 'Doe';
  }

  return $value;
}
add_filter( 'af/field/prefill_value', 'prefill_field_value_example' );

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