【发布时间】:2019-02-03 00:12:01
【问题描述】:
早上好,我在提交表单后使用 Contact Form 7 和电子邮件提交。 现在我需要通过电子邮件发送一个存储在 cookie 中的值(它是一个推荐代码)。我该怎么做?
【问题讨论】:
早上好,我在提交表单后使用 Contact Form 7 和电子邮件提交。 现在我需要通过电子邮件发送一个存储在 cookie 中的值(它是一个推荐代码)。我该怎么做?
【问题讨论】:
您可以查看文档,根据文档,contact form7 不会使用 cookie。
使用默认配置,此插件本身不会:
【讨论】:
以下是实现此目的的步骤。
将此添加到您主题的“functions.php”中。
function dynamictext_cf7_cookie($atts){
extract(shortcode_atts(array(
'key' => -1,
), $atts));
if($key == -1) return '';
$val = '';
if( isset( $_COOKIE[$key] ) ){
$val = $_COOKIE[$key];
}
return $val;
}
add_shortcode('DT_CF7_COOKIE', 'dynamictext_cf7_cookie');
将此添加到您的联系表 7 表单的“表单”选项卡 - [dynamichidden referral-code-field "DT_CF7_COOKIE key='REFERRAL_CODE'"] 其中“REFERRAL_CODE”是您的 PHP cookie 名称。
将此添加到联系表 7 表单的“邮件”选项卡 - [referral-code-field]。
就是这样,您可以在此处阅读更多内容 - https://www.sean-barton.co.uk/2014/04/contact-form-7-place-post-server-cookie-session-variables-fields/。
【讨论】:
我认为您正在尝试做的是在成功提交联系表格 7 后发送另一封电子邮件。您可以通过后端中可用的 wpcf7 挂钩来执行此操作。 wpcf7_mail_sent
注意:您必须获取所需的 cookie 数据,并使用 javascript 将其作为隐藏字段动态包含在表单中,以便将其包含在提交的表单中,您可以在后端检索。
//you can place this in your functions.php
add_action('wpcf7_mail_sent', function ($cf7) {
//do what you want here like get the extra
});
假设我已经使用 javascript/jquery 将 cookie 中的数据添加为隐藏字段,这就是我在后端的操作方式,例如在表单标签内注入<input type="hidden" name="referral_code" />。
add_action('wpcf7_mail_sent', function ($cf7) {
$wpcf7 = WPCF7_ContactForm::get_current();
$submission = WPCF7_Submission::get_instance();
$posted_data = empty($submission) ? null : $submission>get_posted_data();
//assuming you are tracking a form with an id 1234
if($wpcf7->id() === 1234){
//not sure if this still works, if not you can simply use $_GET['referral_code']
if(isset($posted_data['referral_code'])){
$referralCode = $posted_data['referral_code'];
//...now from this point you can send an email or pass this info to another platform for tracking purposes.
}
}
});
【讨论】:
无需任何插件,只需添加新的自定义Special Mail Tag即可实现相同的功能非常简单:
STEP-1:在您的主题的functions.php中添加以下代码
add_filter( 'wpcf7_special_mail_tags', 'wpcf7_my_cookie_mailtag', 10, 3 );
function wpcf7_my_cookie_mailtag( $output, $name, $html ) {
if ( '_my_cookie_special_tag' != $name ) { // rename the tag name as your wish;
return $output;
}
if ( ! $contact_form = WPCF7_ContactForm::get_current() ) {
return $output;
}
$val = isset($_COOKIE['my_cookie']) ? $_COOKIE['my_cookie'] : '';
return $html ? esc_html($val) : $val;
}
STEP-2:在您的Mail Setup 中使用此短代码[_my_cookie_special_tag]。
【讨论】: