anonymized-15380454
(@anonymized-15380454)
event.detail.contactFormId is the contact form ID and not its name.
See https://contactform7.com/dom-events/.
So use an ID, like in this example:
<script>
document.addEventListener( ‘wpcf7mailsent’, function( event ) {
// If the form ID is 3, redirects users to https://www.fusion3design.com/thank/
if ( 3 === event.detail.contactFormId ) {
location = ‘https://www.fusion3design.com/thank/’;
}
}, false );
</script>
For several forms (on the same page), you can use the switch statement, like so:
<script>
document.addEventListener( ‘wpcf7mailsent’, function( event ) {
switch ( event.detail.contactFormId ) {
case 3:
location = ‘https://www.fusion3design.com/thank/’;
break;
case 4:
location = 'URL if form ID is 4';
break;
case 5:
location = 'URL if form ID is 5';
break;
}
}, false );
</script>
Thank you @saltennys!
The switch statement is exactly what I was looking for, thank you.
Unfortunately, like others, I am getting the spinning icon issue so the form is not redirecting as intended. Hopefully when that issue is resolved, then we will see how the script is operating.
anonymized-15380454
(@anonymized-15380454)
You’re welcome.
But I’m not actually getting the spinning icon issue — not with the default code snippet provided and with the latest Contact Form 7 release. So maybe the Contact Form 7 script on your site is in conflict with one or more (JS) scripts?
You can temporarily disable/deactivate all non- Contact Form 7 plugins and see if the issue persists. If not, it could be your theme.
anonymized-15380454
(@anonymized-15380454)
But anyway, there is a plugin which you might want to try:
Contact Form 7 Redirection
I haven’t tried it though.
Thanks @saltennys
Yes, need to spend the time to diagnose the issue.
We used to use the Contact Form 7 Redirection plugin, however, my understanding is that it will no longer work due to the phase out of the on_sent_ok command
anonymized-15380454
(@anonymized-15380454)
I see.
But apart from that plugin and using JavaScript, there is a Contact Form 7 filter you can try:
wpcf7_mail_sent
However, it needs PHP coding, like below (which uses closure), and is not to be used/enabled in AJAX submission mode.
<?php
add_action( 'wpcf7_mail_sent', function( $contact_form_obj ) {
// Redirection logic/"switch" goes here
} );
?>
For an advanced approach, you can copy the Contact Form 7 script, modify it (to add custom redirection logic), and then disable the loading of the original or default JS script.
Thanks and hopefully you’ll be able to find a workaround for that (spinning icon) issue!
I found the offending plugin and CF7 now working fine (although a bit peeved about losing pop-up capabilities for one of my forms)
On an aside, the CF7 redirect plugin still works, although I thought that I read that it will become unusable soon due to phase out of a WP command.
anonymized-15380454
(@anonymized-15380454)
You can always try other pop-up plugins. 🙂