Conditionally redirecting the user to a different page

In this example we will show you how to conditionally redirect the user to different page based on their form selection. We will check the value of a Dropdown Menu element and change the URL redirect for different choices.

Step 1

First you will need to set up the form to redirect by going to Settings → General → Successful submit options. At On successful submit choose Redirect to another page and choose the default page or URL to redirect the user to.

Step 2

Add the following code to the wp-content/themes/YOUR_THEME/functions.php file (or create a plugin for it).

1
2
34
5
678
91011
121314
15
16
17
18
19
function my_conditional_form_redirect($url, $form)
{
    $selection = $form->getValue('iphorm_3_1');    
    switch ($selection) {
        case 'Option 1':            $url = 'http://example.com/page1';            break;
        case 'Option 2':            $url = 'http://example.com/page2';            break;
        case 'Option 3':            $url = 'http://example.com/page3';            break;
    }
    
    return $url;
}
add_action('iphorm_success_redirect_url_3', 'my_conditional_form_redirect', 10, 2);
function my_conditional_form_redirect($url, $form)
{
    $selection = $form->getValue('iphorm_3_1');
    
    switch ($selection) {
        case 'Option 1':
            $url = 'http://example.com/page1';
            break;
        case 'Option 2':
            $url = 'http://example.com/page2';
            break;
        case 'Option 3':
            $url = 'http://example.com/page3';
            break;
    }
    
    return $url;
}
add_action('iphorm_success_redirect_url_3', 'my_conditional_form_redirect', 10, 2);

Modify the above lines to suit the form setup.

  • On line 3, change iphorm_3_1 to the unique element ID of the element you want to check the value of
  • On line 6, change Option 1 to the value of the field that will redirect to the URL on line 7
  • On line 7, change http://example.com/page1 to the redirect URL when the element has the value from line 6
  • On line 9, change Option 2 to the value of the field that will redirect to the URL on line 10
  • On line 10, change http://example.com/page1 to the redirect URL when the element has the value from line 9
  • On line 12, change Option 3 to the value of the field that will redirect to the URL on line 13
  • On line 13, change http://example.com/page1 to the redirect URL when the element has the value from line 12
  • On line 19, change the number 3 to the form ID

You can duplicate or remove any of the case statements to suit your needs. For further customization you will need a PHP developer.

Still having trouble? Head over to the forums.

Forums