Change a field's value during submission based on another value

Created by Phil Kurth, Modified on Thu, 16 Feb, 2023 at 8:28 AM by Phil Kurth

At present, there isn't a convenient filter in place to do this so if you need to modify a field value during submission, you need to modify the $_POST superglobal directly. 


// Note the priority of 5 used on this hook. This is so that the modification fires before Advanced Forms' handler which
// fires on a priority of 10.
add_action( 'init', function () {

	// Make sure a form was posted.
	if ( ! isset( $_POST['af_form'] ) ) {
		return;
	}

	// Check for the form key to only run this on a specific form.
	if ( $_POST['af_form'] !== 'form_63603dfd5f7d2' ) {
		return;
	}

	$field_1_key = 'field_63603df414f2a';
	$field_2_key = 'field_63603e7514f2b';

	// Check the value of field 1 matches a condition and if so, modify the value of field 2.
	if ( isset( $_POST['acf'][ $field_1_key ] ) and $_POST['acf'][ $field_1_key ] === 'some value' ) {
		$_POST['acf'][ $field_2_key ] = 'field 2 updated value';
	}

}, 5 );

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