Redirect Gravity Forms Based on an Answer
Sends people to a different page after submitting a Gravity Forms form depending on what they chose in one field, such as a sales or support thank-you page.
Opens WPIDE on
Code
$wpide_gf_redirect_form = 0; // Form ID (0 = off until you set it).
$wpide_gf_redirect_field = 6; // Field ID holding the answer.
// Answer value => URL.
$wpide_gf_redirect_map = array(
'sales' => '/thank-you-sales/',
'support' => '/thank-you-support/',
);
add_filter( 'gform_confirmation_' . $wpide_gf_redirect_form, function ( $confirmation, $form, $entry ) use ( $wpide_gf_redirect_field, $wpide_gf_redirect_map ) {
$answer = (string) rgar( $entry, (string) $wpide_gf_redirect_field );
if ( isset( $wpide_gf_redirect_map[ $answer ] ) ) {
return array( 'redirect' => esc_url_raw( home_url( $wpide_gf_redirect_map[ $answer ] ) ) );
}
return $confirmation;
}, 10, 3 );
Notes
Set the form ID, field ID and the value-to-URL map at the top; other answers keep the form's normal confirmation.
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.