Redirect WPForms Based on an Answer
Sends visitors to a different thank-you page after a WPForms form depending on the option they picked, such as separate pages for sales and support.
Opens WPIDE on
Code
$wpide_wpf_redirect_form = 0; // Form ID (0 = off until you set it).
$wpide_wpf_redirect_field = 6; // Field ID with the answer.
// Answer label => URL.
$wpide_wpf_redirect_map = array(
'Sales' => '/thank-you-sales/',
'Support' => '/thank-you-support/',
);
add_filter( 'wpforms_process_redirect_url', function ( $url, $form_id, $fields ) use ( $wpide_wpf_redirect_form, $wpide_wpf_redirect_field, $wpide_wpf_redirect_map ) {
if ( (int) $form_id !== $wpide_wpf_redirect_form ) {
return $url;
}
$answer = isset( $fields[ $wpide_wpf_redirect_field ]['value'] ) ? (string) $fields[ $wpide_wpf_redirect_field ]['value'] : '';
return isset( $wpide_wpf_redirect_map[ $answer ] ) ? esc_url_raw( home_url( $wpide_wpf_redirect_map[ $answer ] ) ) : $url;
}, 10, 3 );
Notes
Set the form ID, field ID and map at the top, and set the form's confirmation type to "Go to URL (Redirect)" with a fallback URL that this snippet swaps by answer.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.