Last updated on October 18, 2023

WP Forms conditional redirects

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Redirect users based on form responses.

Conditional redirects in WP Forms can be useful when you want to redirect users to different pages based on their form submission. This can be used for various purposes such as redirecting users to a thank you page, showing different confirmation messages, or redirecting users to specific pages based on their selections in the form.

To achieve conditional redirects in WP Forms, you can use the wpforms_process_complete hook along with a custom PHP function. Here’s an example code snippet that demonstrates how to redirect users based on a specific form field value:

/**
 * Redirect users based on form field value.
 *
 * @param array $fields
 * @param array $entry
 * @param array $form_data
 *
 * @return void
 */
function wpsnippets_redirect_based_on_form_field( $fields, $entry, $form_data ) {
    // Replace 'form_field_name' with the actual name or ID of your form field.
    $form_field_value = $entry['fields']['form_field_name']['value'];

    // Replace 'desired_value' and 'redirect_url' with your desired values.
    if ( $form_field_value === 'desired_value' ) {
        wp_redirect( 'https://example.com/redirect-url' );
        exit;
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_redirect_based_on_form_field', 10, 3 );

In the above code, we’re using the wpforms_process_complete hook to execute our custom function wpsnippets_redirect_based_on_form_field after the form submission is processed. Inside the function, we retrieve the value of the form field using the $entry parameter. Then, we check if the form field value matches our desired value. If it does, we use wp_redirect() to redirect the user to the specified URL.

Remember to replace 'form_field_name' with the actual name or ID of your form field, 'desired_value' with the value you want to check against, and 'https://example.com/redirect-url' with the URL you want to redirect the user to.

This code snippet can be useful when you want to create dynamic form submissions that redirect users based on their selections or input.

Examples

Example 1: Redirect to a specific page based on form submission

This use case demonstrates how to redirect users to a specific page after submitting a form, based on certain conditions. In this example, we will redirect users to a “Thank You” page if they select a specific option in a dropdown field.

add_action( 'wpforms_process_complete', 'wpsnippets_redirect_after_form_submission', 10, 4 );
function wpsnippets_redirect_after_form_submission( $fields, $entry, $form_data, $entry_id ) {
    $selected_option = wpforms()->process->fields['dropdown_field']->get_value();

    if ( $selected_option === 'specific_option' ) {
        wp_redirect( home_url( '/thank-you/' ) );
        exit;
    }
}

Explanation:

  • We hook into the wpforms_process_complete action to execute our custom function after the form submission is processed.
  • We retrieve the value of the dropdown field using the get_value() method.
  • If the selected option matches our condition, we use wp_redirect() to redirect the user to the specified URL (in this case, the “Thank You” page).
  • We include exit to ensure that the redirect happens immediately.

Example 2: Redirect to different URLs based on form field values

In this use case, we will redirect users to different URLs based on the values they enter in specific form fields. This can be useful for creating personalized experiences or directing users to relevant content.

add_action( 'wpforms_process_complete', 'wpsnippets_redirect_based_on_form_fields', 10, 4 );
function wpsnippets_redirect_based_on_form_fields( $fields, $entry, $form_data, $entry_id ) {
    $field_value = wpforms()->process->fields['text_field']->get_value();

    switch ( $field_value ) {
        case 'value1':
            wp_redirect( home_url( '/page1/' ) );
            exit;
        case 'value2':
            wp_redirect( home_url( '/page2/' ) );
            exit;
        default:
            wp_redirect( home_url( '/default-page/' ) );
            exit;
    }
}

Explanation:

  • We use the wpforms_process_complete action to trigger our custom function after the form submission is processed.
  • We retrieve the value of a specific form field using the get_value() method.
  • Using a switch statement, we check the value of the field and redirect the user to the corresponding URL based on the condition.
  • If none of the conditions match, we redirect the user to a default page.

Example 3: Redirect to a URL based on multiple form field values

This use case demonstrates how to redirect users to a specific URL based on multiple form field values. It allows for more complex conditional redirects based on various combinations of form field values.

add_action( 'wpforms_process_complete', 'wpsnippets_redirect_based_on_multiple_fields', 10, 4 );
function wpsnippets_redirect_based_on_multiple_fields( $fields, $entry, $form_data, $entry_id ) {
    $field1_value = wpforms()->process->fields['field1']->get_value();
    $field2_value = wpforms()->process->fields['field2']->get_value();

    if ( $field1_value === 'value1' && $field2_value === 'value2' ) {
        wp_redirect( home_url( '/page1/' ) );
        exit;
    } elseif ( $field1_value === 'value3' && $field2_value === 'value4' ) {
        wp_redirect( home_url( '/page2/' ) );
        exit;
    } else {
        wp_redirect( home_url( '/default-page/' ) );
        exit;
    }
}

Explanation:

  • We hook into the wpforms_process_complete action to execute our custom function after the form submission is processed.
  • We retrieve the values of two different form fields using the get_value() method.
  • Using an if statement with multiple conditions, we check the combination of field values and redirect the user to the corresponding URL based on the condition.
  • If none of the conditions match, we redirect the user to a default page.
Last updated on October 18, 2023. Originally posted on November 12, 2023.

Leave a Reply