你们知道在CF7中 on_sent_ok
命令已经废弃,计划在2017年底前废除。所以我决定用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');
但这适用于所有的联系表单。由于我使用的表单类型相当不同,请问如何才能将其中一个表单排除在这个重定向之外?
试试这个脚本。
<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>
奖励提示: 我经常用另一种方式来做,让它更灵活一些。我把一个 <div class="do-some-action" data-something="foobar" style="display:none;"></div>
在CF7表格本身,然后如果需要的话,我可以把这个动作放在多个表格中。
<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>
希望能帮到你