简体   繁体   中英

Contact form 7 - URL redirection

As you guys know in CF7 on_sent_ok command deprecated and scheduled to be abolished by the end of 2017. So I decided to use the new script for redirecting my contact forms with this script provided by CF7

function add_this_script_footer(){ ?>

<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    location = 'http://websiteurl/thank-you';
}, false );
</script>

<?php } 

add_action('wp_footer', 'add_this_script_footer'); 

but this applies to all contact forms. Since I am using quite different types of forms, may I know how can I exclude one of them from this redirection?

Try this script:

<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    if (event.detail.contactFormId != '123') { // This would exclude form with id 123
        location = 'http://websiteurl/thank-you';
    }
}, false );
</script>

Bonus tip: I often do it another way that makes it a bit more flexible. I put a <div class="do-some-action" data-something="foobar" style="display:none;"></div> in the CF7 form itself and then I can put this action in multiple forms if needed..

<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
    var $cf = $( '#' + event.detail.id );
    var $actionDiv = $cf.find( '.do-some-action' );
    if ( $actionDiv && $actionDiv.length ) {
        // Div with action class found
        // We can also extract some data if needed
        var something = $actionDiv.data( 'something' );
        console.log( 'something = ' + something );
        location = 'http://websiteurl/thank-you';
    }
}, false );
</script>

I hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM