You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

开启skip_mail:on时如何实现Contact Form7表单跳转至指定页面?

Contact Form7 开启skip_mail后实现表单提交跳转专属感谢页

开启skip_mail:on后,原代码依赖的wpcf7mailsent事件不会触发(该事件仅在邮件发送完成后触发),因此无法实现跳转。以下两种方法可在无需启用邮件发送的情况下完成需求:

方法一:使用前端wpcf7submit事件

wpcf7submit事件在表单提交验证通过后立即触发,不受邮件发送设置影响。将原代码中的事件替换为该事件即可:

add_action( 'wp_footer', 'redirect_cf7_without_mail' );
 
function redirect_cf7_without_mail() {
?>
<script type="text/javascript">
document.addEventListener( 'wpcf7submit', function( event ) {
    // 仅在表单验证通过后执行跳转
    if (event.detail.status === 'ok') {
        const formId = event.detail.contactFormId;
        if ( '4380' == formId ) { 
            location.href = 'https://domain_here.com/create-processing/?type=video';
        } else if ( '4086' == formId ) { 
            location.href = 'https://domain_here.com/create-processing/?type=channel';
        } else { 
            location.href = 'https://domain_here.com/create-processing/?type=none';
        }
    }
}, false );
</script>
<?php
}

方法二:使用后端PHP钩子wpcf7_submit

通过后端钩子实现跳转,不受前端JS禁用影响,可靠性更高:

add_action( 'wpcf7_submit', 'cf7_redirect_after_submit', 10, 2 );

function cf7_redirect_after_submit( $contact_form, $result ) {
    // 表单验证通过时执行跳转
    if ( $result['status'] == 'ok' ) {
        $form_id = $contact_form->id();
        $redirect_url = '';
        
        switch ($form_id) {
            case 4380:
                $redirect_url = 'https://domain_here.com/create-processing/?type=video';
                break;
            case 4086:
                $redirect_url = 'https://domain_here.com/create-processing/?type=channel';
                break;
            default:
                $redirect_url = 'https://domain_here.com/create-processing/?type=none';
                break;
        }
        
        if ( !empty($redirect_url) ) {
            wp_redirect( $redirect_url );
            exit;
        }
    }
}

注意事项

  • 替换代码中的https://domain_here.com/...为实际的感谢页URL。
  • 确保表单ID(4380、4086)与你网站上的CF7表单ID一致。

内容的提问来源于stack exchange,提问作者Jamie Warren

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.13 02:55:30