自 WPCF7 5.2 以来,wpcf7_before_send_mail 挂钩发生了很大变化。作为参考,这里是如何在 5.2+ 中使用这个钩子
跳过邮件
function my_skip_mail() {
return true; // true skips sending the email
}
add_filter('wpcf7_skip_mail','my_skip_mail');
或将skip_mail 添加到管理区域表单上的其他设置选项卡中。
获取表单 ID 或帖子 ID
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
$post_id = $submission->get_meta('container_post_id');
$form_id = $contact_form->id();
// do something
return $contact_form;
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
获取用户输入的数据
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
$your_name = $submission->get_posted_data('your-field-name');
// do something
return $contact_form;
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
将电子邮件发送给动态收件人
function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
$dynamic_email = 'email@email.com'; // get your email address...
$properties = $contact_form->get_properties();
$properties['mail']['recipient'] = $dynamic_email;
$contact_form->set_properties($properties);
return $contact_form;
}
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );