正如你们在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'); 但这适用于所有的联系人表单。由于我使用的是完全不同类型的表单,我可以知道如何从这个重定向中排除其中的一个吗?
发布于 2020-05-03 01:01:15
尝试以下脚本:
<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>奖励提示:我经常用另一种方式来使它更灵活一些。我在CF7表单本身中放置了一个<div class="do-some-action" data-something="foobar" style="display:none;"></div>,然后如果需要,我可以将此操作放在多个表单中。
<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>我希望这能帮到你!
https://stackoverflow.com/questions/46192208
复制相似问题