Conditional Redirects in Contact Form 7 – A JavaScript Code Example

Explore our JavaScript category, where you’ll find loads of helpful articles and tutorials for everyone, whether you’re just starting out or a pro developer. Learn about different JavaScript functions that can be used in various projects according to their specific requirements.
JavaScript code snippet using the addEventListener function to listen for the ‘wpcf7mailsent‘ event in a Contact Form 7 (CF7) form. When the form is submitted, the code checks if the form ID is either ‘167’ or ‘115’. If the condition is met, it redirects the user to ‘’.
document.addEventListener(‘wpcf7mailsent’, function (event) {
// Check if the contactFormId is either ‘167’ or ‘115’
if (‘167’ == event.detail.contactFormId || ‘115’ == event.detail.contactFormId) {
// Redirect to ‘’
location = ‘’;
}, false);
This code is commonly used in WordPress environments with the Contact Form 7 plugin. It triggers after the form submission is successful (wpcf7mailsent event) and performs the specified action based on the form ID. In this case, it redirects the user to ‘‘ if the form ID is either ‘167’ or ‘115’.