Tech
Contact Form 7 Redirect After Submit: on_sent_ok Replacement
on_sent_ok no longer works in Contact Form 7. Redirect to a thank-you page with the wpcf7mailsent event, per form, without losing GA4 conversions.
Sending people to a dedicated thank-you page after a Contact Form 7 (CF7) submission is one of the most common requests we get. Ad platforms want a URL to count conversions, and marketing teams want a page that shows a download link or next steps.
The trouble is that most of the answers you find in search still tell you to put on_sent_ok: "location = 'https://example.com/thanks/';" in the Additional Settings tab. That line does nothing in the current version of Contact Form 7. The form submits, the email arrives, and the visitor stays exactly where they were.
This article shows the method that works today, how to send each form to its own page, and how to keep the GA4 conversion from disappearing when the page changes. Everything below is based on the plugin’s own source code, which we downloaded from WordPress.org and read line by line.
What we checked, and what changed
On November 4, 2026 we downloaded two builds from downloads.wordpress.org:
| Build | md5 of the zip | What happens to on_sent_ok |
|---|---|---|
| Contact Form 7 4.9.2 | c7465f0a9bd563d8d349cc4598a88a6c | Read in includes/contact-form.php and returned to the browser as scripts_on_sent_ok, which the front-end script ran after a successful send |
| Contact Form 7 6.2.4.1 (current) | df9fd5571996b7007471852a8f2cd36c | Read only in includes/config-validator/additional-settings.php, which reports “Deprecated settings are used.” Nothing executes it |
The current build declares Requires PHP: 8.3 and Requires at least: 7.1 (WordPress), so it is worth checking your server before you update. We read the code with PHP 8.3.6.
Two practical consequences follow from the table:
- If you added
on_sent_okyears ago and it “suddenly stopped working,” it did not break. The feature was removed, and your line is now a comment that the plugin ignores. - The configuration validator does notice it. It reports “Deprecated settings are used.” for the Additional Settings section of that form. Remove the line so the warning goes away and nobody on your team tries to fix it later.
The replacement is a DOM event. In 6.2.4.1, the front-end script (includes/js/index.js) creates every event with new CustomEvent("wpcf7" + name, { bubbles: true, detail: ... }). Because the events bubble, one listener on document catches them from every form on the page.
The events Contact Form 7 fires
After the AJAX request comes back, the script maps the server’s status to an event. From the 6.2.4.1 source:
| Server status | Event fired | Fires on |
|---|---|---|
mail_sent | wpcf7mailsent | Successful send only |
mail_failed | wpcf7mailfailed | The email could not be sent |
validation_failed | wpcf7invalid | A field failed validation |
acceptance_missing | wpcf7unaccepted | The acceptance checkbox was not ticked |
spam | wpcf7spam | The submission was judged as spam |
| (any) | wpcf7submit | Every submission, whatever the result |
Before the request, the script also fires wpcf7beforesubmit.
For a redirect, listen for wpcf7mailsent and nothing else. We still see sites that use wpcf7submit, which means a visitor who mistypes their email address is sent to the “Thank you, we received your message” page. They leave believing they contacted you, and you never hear from them.
Method 1: redirect every form to one page
Add this to your theme’s JavaScript file, or print it in the footer with a small plugin or your theme’s functions.php. It has to load on every page that shows a form.
document.addEventListener( 'wpcf7mailsent', function () {
window.location.href = 'https://example.com/thanks/';
}, false );
That is all you need when the site has one contact form.
If something is wrong with email delivery, this code will (correctly) not redirect, and the visitor will see the error message instead. That is a separate problem. Our guide on Contact Form 7 not sending emails walks through the SMTP and SPF/DKIM checks.
Method 2: a different thank-you page for each form
The event carries a detail object. In 6.2.4.1 it contains contactFormId, pluginVersion, contactFormLocale, unitTag, containerPostId, status, inputs, formData and apiResponse.
contactFormId is the form’s post ID, the same number you see in the shortcode [contact-form-7 id="123" ...]. That is what you use to choose the destination:
document.addEventListener( 'wpcf7mailsent', function ( event ) {
var destinations = {
123: 'https://example.com/thanks-contact/',
456: 'https://example.com/thanks-quote/',
789: 'https://example.com/thanks-download/'
};
var url = destinations[ event.detail.contactFormId ];
if ( url ) {
window.location.href = url;
}
}, false );
Forms that are not in the list keep the normal behaviour and show their success message. That fallback matters: a newsletter form in the footer should not throw visitors onto your sales thank-you page.
Avoid matching on the form title. Titles get edited by whoever manages the site, and the redirect then breaks without any error. The ID only changes if the form is deleted and created again.
If the same form appears on several pages and you want different destinations per page (for example, a quote form on two service pages), use event.detail.containerPostId, which is the ID of the page that contains the form.
Method 3: redirect without losing the GA4 conversion
This is where most implementations quietly lose data. When you set window.location.href immediately, the browser starts unloading the page, and any GA4 request still in flight can be cancelled. The lead reaches your inbox, but GA4 records nothing.
There are two safe patterns. Pick one.
Pattern A: send the event first, then redirect
document.addEventListener( 'wpcf7mailsent', function ( event ) {
var url = 'https://example.com/thanks/';
var done = false;
function go() {
if ( done ) {
return;
}
done = true;
window.location.href = url;
}
if ( typeof gtag === 'function' ) {
gtag( 'event', 'generate_lead', {
form_id: String( event.detail.contactFormId ),
event_callback: go
} );
// If the GA4 request is blocked (ad blockers, consent mode),
// do not leave the visitor waiting.
setTimeout( go, 1000 );
} else {
go();
}
}, false );
event_callback runs once GA4 has sent the hit. The timeout is the safety net: if an ad blocker or a consent banner stops GA4, the callback never runs, and without the timeout the visitor would be stuck on the form page. One second is enough in practice and still feels instant.
Pattern B: count the conversion on the thank-you page
Leave the form page alone and mark the thank-you page as the conversion in GA4 (a key event triggered by page_view on that URL) or in your ad platform. This is the simplest option when an ad network insists on a destination URL.
Do not combine the two patterns, or every lead is counted twice. Also add noindex to the thank-you page so it does not appear in search results, where people could land on it without submitting anything and inflate your conversions.
If the thank-you page has no other purpose, ask whether you need a redirect at all. Sending the GA4 event from wpcf7mailsent and keeping the visitor on the page is the most accurate setup, because there is nothing to cancel.
WordPress Plugin
Set the thank-you page per form, without JavaScript.
Frontierline Forms adds a confirmation screen, a completion screen and a redirect URL to each Contact Form 7 form through its settings screen. The redirect page can be used as a GA4 conversion point, and the confirmation screen, multi-step flow and submission log storage are free and unlimited. The plugin is distributed through the official WordPress.org repository.
Common reasons the redirect still does not happen
When the code above is in place and nothing happens, it is almost always one of these:
- The script never loads. Some caching and optimization plugins delay or combine scripts. Reload the form page with the browser’s developer tools open and check that your file appears in the Network tab.
- The form is not submitting through AJAX. If the page reloads on submit, Contact Form 7’s JavaScript is not running (often a JavaScript error from another plugin earlier on the page). No AJAX means no events. Check the console for red errors.
- The submission did not succeed. Watch the message under the form. If it shows a validation or sending error,
wpcf7mailsentwas never fired, which is the intended behaviour. - The ID in your map is wrong. Log it once with
console.log( event.detail.contactFormId )inside awpcf7submitlistener and compare. The value is a number, and object keys in the map above match it either way. - A confirmation-screen plugin changes the flow. With a confirmation step, the first click shows the review screen and only the second click actually sends. Test the whole flow once with a
console.login your listener to confirm thatwpcf7mailsentfires on the final send. We cover how the confirm step fits into Contact Form 7 in our confirmation screen guide.
Summary
| Goal | What to use |
|---|---|
| One thank-you page for all forms | wpcf7mailsent + window.location.href |
| A different page per form | event.detail.contactFormId mapped to URLs |
| A different page per placement | event.detail.containerPostId |
| Keep the GA4 conversion | Send the event first with event_callback and a timeout, or count on the thank-you page (not both) |
on_sent_ok was removed, not broken. The current Contact Form 7 only warns you that it is there. Replace it with a wpcf7mailsent listener, never with wpcf7submit, and decide whether you measure on the form page or the thank-you page before you write any code.
WordPress Plugin
Confirmation screen, completion screen and redirect, free.
Keep your existing Contact Form 7 forms and add an "input → confirm → submit → complete" flow from the settings screen. If you also need Slack or Chatwork notifications, the Pro version starts at $39.99/year.