在我们的网站上成功提交CF7表格后,客户将被重定向到感谢页面。但我们只希望他们在提交特定表格时转到感谢页面。
到目前为止,我只能使代码为特定页面而不是特定表单执行此操作。是否有可能重写或以某种方式改变它?
//CF7 redirect code
function cf7_footer_script(){
//If the page is called
if ( is_page('bestil-knivslibning')) {?>
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = 'https://skarpeknive.dk/thank-you/';
}, false );
</script>
<?php } else if ( is_page('forside')) /* if the page is called */ {?>
<script>
document.addEventListener( 'wpcf7mailsent', function( event ) {
location = 'https://skarpeknive.dk/thank-you/';
}, false );
</script>
<?php }
}
add_action('wp_footer', 'cf7_footer_script');
答案 0 :(得分:1)
您需要为每个if条件指定重定向的ID。
我建议您为每个表单重定向特定的表单ID。 只需用短代码中的表单ID替换154
例如: [contact-form-7 id="154" title="Contact form 1"]
使用最新的CF7测试代码并正常运行。
add_action('wp_footer', 'cf7_specific_form_redirect');
function cf7_specific_form_redirect()
{
?>
<script type="text/javascript">
document.addEventListener('wpcf7mailsent', function (event) {
if (154 === event.detail.contactFormId) { // Sends sumissions on form 947 to the first thank you page
location = 'https://www.example.com/thank-you-1/';
} else if (155 === event.detail.contactFormId) { // Sends submissions on form 1070 to the second thank you page
location = 'https://www.example.com/thank-you-2/';
} else if (156 === event.detail.contactFormId) { // Sends submissions on form 1070 to the second thank you page
location = 'https://www.example.com/thank-you-3/';
}
}, false);
</script>
<?php
}