Redirect to “Thank You Page” after success sent mail

Redirecting to a Thank You page after Contact Form 7 submission

Contact Form 7 doesn’t include a built-in redirect option. The plugin author argues that modern Analytics tracking makes redirects unnecessary, but clients often still want one. Here are two copy-paste-ready approaches — one server-side, one front-end.

Problem: We need to redirect users to a “Thank You” page after a successful Contact Form 7 submission.

Solution: Hook into the wpcf7_mail_sent action for a server-side PHP redirect (no JavaScript required, but no AJAX support), or listen for the wpcf7mailsent DOM event in JavaScript for an AJAX-compatible front-end redirect that works with relative URLs.

The developer of Contact Form 7 has written:

I’m often asked how to redirect to a “Thank You” page. In most cases, people want this for Google Analytics tracking — but that’s not necessary at all. Today you can track form submissions with Google Analytics without any redirection. It’s an outdated approach.

That said, if clients want it, we build it. Here are two working, copy-paste-ready solutions — each with its own trade-offs.

Approach 1: server-side redirect with wpcf7_mail_sent

This works only when the form is submitted without JavaScript (a classic POST with a page reload). Contact Form 7 normally submits forms through its REST endpoint, and a server-side redirect breaks that response, so on most sites you need Approach 2. The redirect URL must be absolute.

<?php
add_action( 'wpcf7_mail_sent', 'redirect_to_success_page' );

function redirect_to_success_page( $contact_form ) {
    $redirect_location = get_field( 'redirect_location', 'options' );
    $redirect_status   = get_field( 'redirect_status', 'options' );

    if ( $redirect_location && $redirect_status ) {
        wp_redirect( $redirect_location, $redirect_status );
        exit;
    }
}

Approach 2: front-end redirect with the wpcf7mailsent event

Works with the default AJAX submission and accepts relative URLs:

<?php
add_action( 'wp_footer', 'redirect_to_success_page_js' );

function redirect_to_success_page_js() {
    $redirect_location = get_field( 'redirect_location', 'options' );
    if ( ! $redirect_location ) {
        return;
    }
    ?>
    <script>
        document.addEventListener( 'wpcf7mailsent', function( event ) {
            window.location.href = <?php echo wp_json_encode( esc_url_raw( $redirect_location ) ); ?>;
        }, false );
    </script>
    <?php
}

NOTE: Both examples use ACF option fields, which lets you manage the redirect URL from the admin dashboard without touching code.

location.replace() vs location.href

Use replace() if the visitor should not come back to the form with the Back button:

// Behaves like an HTTP redirect (replaces history entry)
window.location.replace( 'https://example.com' );

// Behaves like clicking a link (adds to history)
window.location.href = 'https://example.com';

Contact Form 7 DOM events

wpcf7invalid — submission completed, but mail not sent due to invalid fields.
wpcf7spam — submission completed, but mail blocked as possible spam.
wpcf7mailsent — submission completed and mail sent successfully.
wpcf7mailfailed — submission completed, but mail sending failed.
wpcf7submit — submission completed, regardless of outcome.

A different Thank You page for each form

Listen on document and pick the URL by the form ID from event.detail.contactFormId. One listener covers every form on the page, including a second copy of the same form (for example, in a modal window). A listener attached to a single form element would miss it, because Contact Form 7 gives every copy its own element ID.

// Contact Form 7 form ID => Thank You page
var thankYouPages = {
    4:   '/thank-you/',
    457: '/thanks-for-subscribing/'
};

document.addEventListener( 'wpcf7mailsent', function( event ) {
    var url = thankYouPages[ event.detail.contactFormId ];

    if ( url ) {
        window.location.href = url;
    }
}, false );

Sources:

  1. https://contactform7.com/redirecting-to-another-url-after-submissions
  2. https://contactform7.com/tracking-form-submissions-with-google-analytics
  3. https://contactform7.com/dom-events