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
相关产品推荐
相关产品推荐

