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

WordPress Contact Form7:before_send hook中skip_mail使用报错求助

Contact Form7 跳过邮件发送的代码错误修复

原代码存在的问题:

  • wpcf7_before_send_mail钩子仅接收1个参数(当前表单对象),但你设置了3作为参数数量,参数不匹配触发致命错误
  • 函数内返回了未定义的$contact_form变量,且该钩子是action类型,无需返回值
  • 无需重复调用WPCF7_ContactForm::get_current(),钩子已直接传入表单对象

修正后的代码:

function cornerstonePageFlow($WPCF7_ContactForm) {
    // 仅处理ID为10770的表单
    if ($WPCF7_ContactForm->id == 10770) {
        $submission = WPCF7_Submission::get_instance();
        // 先验证提交实例是否存在,避免无提交场景报错
        if ($submission) {
            $email = $submission->get_posted_data('email');
            if (!empty($email)) {
                $submission->skip_mail = true; // 跳过CF7默认邮件发送流程
            }
        }
    }
}
add_action("wpcf7_before_send_mail", "cornerstonePageFlow", 10, 1);

关键说明:

  • 调整add_action的第四个参数为1,匹配钩子的参数数量
  • 增加$submission的存在判断,提升代码健壮性
  • $submission->skip_mail = true是Contact Form7中跳过默认邮件发送的官方支持方式,可在wpcf7_before_send_mail钩子中正常使用

内容的提问来源于stack exchange,提问作者Syed Haris Ali Ghaznavi

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.04 20:35:14