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. This works purely server-side — no JavaScript required, which can matter in some contexts. The trade-off is that AJAX is not supported (the page reloads on re-submission), and 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. Supports AJAX and 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 esc_url( $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.
JavaScript redirect methods for reference:
// 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.
To trigger different redirects per form, use the form ID from the event detail:
var wpcf7Elm = document.querySelector( '.wpcf7' );
wpcf7Elm.addEventListener( 'wpcf7submit', function( event ) {
console.log( 'Form submitted, ID:', event.detail.contactFormId );
}, false );
Sources: