gpt4 book ai didi

php - 动态更改 CF7 表单值

转载 作者:可可西里 更新时间:2023-10-31 22:06:09 24 4
gpt4 key购买 nike

我一直在尝试动态更改 CF7 表单字段而不使用 Contact Form 7 动态文本扩展。我看过很多关于如何获取发布数据的文章,而不是关于如何覆盖现有值的文章。我的目标是动态更改文件附件并添加与每个帖子关联的其他元数据。这可以做到吗?谢谢!

这是我目前所拥有的:

function wpcf7_custom_before_send(&$cf7) {
if ( $cf7->id == 4 ) {
$submission = WPCF7_Submission::get_instance();
if ( $submission ) {
$data =& $submission->get_posted_data();
// how do I overwrite posted data?
}
}
}
add_action("wpcf7_before_send_mail", "wpcf7_custom_before_send");

最佳答案

您可以使用我的代码来执行此操作。对您的代码的一些解释:

1) 由于 id $cf7->id 属性不再可访问。使用 id() 方法代替 $cf7->id()

2) 回调$cf7$submission 不需要使用&。用于此返回

add_action("wpcf7_before_send_mail", "wpcf7_do_something");

function wpcf7_do_something($WPCF7_ContactForm)
{

if (224 == $WPCF7_ContactForm->id()) {

//Get current form
$wpcf7 = WPCF7_ContactForm::get_current();

// get current SUBMISSION instance
$submission = WPCF7_Submission::get_instance();

// Ok go forward
if ($submission) {

// get submission data
$data = $submission->get_posted_data();

// nothing's here... do nothing...
if (empty($data))
return;

// extract posted data for example to get name and change it
$name = isset($data['your-name']) ? $data['your-name'] : "";

// do some replacements in the cf7 email body
$mail = $wpcf7->prop('mail');

// Find/replace the "[your-name]" tag as defined in your CF7 email body
// and add changes name
$mail['body'] = str_replace('[your-name]', $name . '-tester', $mail['body']);

// Save the email body
$wpcf7->set_properties(array(
"mail" => $mail
));

// return current cf7 instance
return $wpcf7;
}
}
}

就是这样,我们更改了一些标签,并发送带有修改后标签的电子邮件;-)

关于php - 动态更改 CF7 表单值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25817442/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com