How to use a form to manage ACF fields on an options page

Created by Phil Kurth, Modified on Thu, 30 Jun, 2022 at 10:32 AM by Phil Kurth

Saving to an options page is not currently a built-in feature but it is definitely possible. 

Saving field values to options

If you use the same field group for both your form and options page, this should be enough to save the submitted data from the form to the options:

// Hook into the submission handler and save all fields as 'options'.
add_action( 'af/form/submission', function ( $form, $fields, $args ) {
  if ( $form['key'] === 'YOUR_FORM_KEY_HERE' ) {
    af_save_all_fields( 'options' );
  }
}, 10, 3 );

Loading field values from options

You probably also want to pre-fill the form with values from the options page to make it a full editing experience. For that you can use a snippet like this:

// Hook into the prefill filter to load values from 'options' when displaying the form.
add_filter( 'af/field/prefill_value', function ( $value, $field, $form, $args ) {
  if ( $form['key'] === 'YOUR_FORM_KEY_HERE' ) {
    return get_field( $field['key'], 'options' );
  }
  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