Dynamically/conditionally set a redirect on form submission

Created by Phil Kurth, Modified on Thu, 17 Nov, 2022 at 4:45 PM by Phil Kurth

If you need to set or override the redirect behaviour on a form based on a submitted value, you may do so using the af/form/submission filter

The objective here is not to modify the form object but to instead modify the args on the current submission. By setting the 'redirect' flag in this fashion, we can enforce a redirect to any URL we need including a dynamically generated one.


<?php

add_action( 'af/form/submission', function ( $form, $field, $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 $form;
  }

  // Get a field value from the submission.
  $value = af_get_field( 'some_field_name' );

  // If the value exists, set the redirect arg on the form submission.
  if ( $value ) {
    AF()->submission['args']['redirect'] = '/some/page?data=' . esc_attr( $value );
  }

  return $form;
}, 20, 3 );

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