我试图从自定义字段动态抓取收件人电子邮件,并使用字符串替换来修改联系人表单7收件人电子邮件。联系表单正在发送,但似乎没有更改收件人电子邮件,因为我没有收到电子邮件。
<?php
function wpcf7_dynamic_email_field( $args ) {
$dynamic_email = '';
$submission = WPCF7_Submission::get_instance();
$unit_tag = $submission->get_meta( 'wpcf7-f3936-p3933-o1' );
// get the post ID from the unit tag
if ( $unit_tag && preg_match( '/^wpcf7-f('d+)-p('d+)-o('d+)$/', $unit_tag, $matches ) ) {
$post_id = absint( $matches[2] );
$dynamic_email = get_post_meta( $post_id, 'email', true );
}
if ( $dynamic_email ) {
$args['recipient'] = str_replace('emailtoreplace@email.com', $dynamic_email, $args['recipient']);
}
return $args;
}
add_filter( 'wpcf7_mail_components', 'wpcf7_dynamic_email_field' );
?>
我正在运行CF7 4.5.1和PHP 5.3我在这里错过了什么?
要向WPCF7 5.2版本的动态收件人发送电子邮件,我是这样做的:
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 );
你想用unit标签做什么不是很清楚,但是,这里有一种解决问题的方法,
function wpcf7_dynamic_email_field( $args ) {
//create a hidden field with your post-id
$dynamic_email = '';
if(isset($_POST['post-id'])){
$post_id = $_POST['post-id'];
$dynamic_email = get_post_meta( $post_id, 'email', true );
}
if ( $dynamic_email ) {
$args['recipient'] = str_replace('emailtoreplace@email.com', $dynamic_email, $args['recipient']);
}
return $args;
}
add_filter( 'wpcf7_mail_components', 'wpcf7_dynamic_email_field' );
为了获得post-id填充,您可以在客户端使用javascript,或者您可以在表单加载时使用CF7动态文本扩展(参见这里的教程)将其预加载。