1. Home
  2. Snippets
  3. Create Conditional Redirects for Form Confirmation

Create Conditional Redirects for Form Confirmation

Heads up!

This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

Dismiss

A form’s confirmation setting determines what the user sees immediately after the form is submitted, and one confirmation option is to redirect the user to a specific URL. In some cases, it may be useful to conditionally redirect users to different URLs based on their selections in the form.

To get started, you’ll first need to set the form’s confirmation to the redirect option. To do this, open the form builder and go to Settings ยป Confirmation. From the Confirmation Type dropdown, you can then select Go to URL (Redirect).

Confirmation settings in WPForms

Next, you’ll need to modify the code below to fit your condition(s).

/**
 * WPForms conditional rules for confirmation redirect
 *
 * @param string $url URL form will redirect to
 * @param int $form_id Form ID
 * @param array $fields Submitted form fields
 * @return string
 */
function wpf_dev_conditional_redirect( $url, $form_id, $fields ) {
	
	// Proceed only if the form ID is 50
	if ( '50' == $form_id ) {
		// If the value of field ID #5 is 'test', then change the redirect URL
		if ( !empty( $fields['5']['value'] ) && 'test' == $fields['5']['value'] ) {
			$url = 'https://wpforms.com';
		}
	}
	return $url;
}
add_filter( 'wpforms_process_redirect_url', 'wpf_dev_conditional_redirect', 10, 3 );

In the code example above, we’re specifically targeting the form with an ID number of 50. Then we’re checking whether the field with an ID of 5 has a value of ‘test’.

If the value matches, the user will be redirected to https://wpforms.com. If the value doesn’t match, the redirect URL from the form’s confirmation settings will be used.

Updated on November 20, 2017

Was this article helpful?