首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >动态更改CF7表单值

动态更改CF7表单值
EN

Stack Overflow用户
提问于 2014-09-12 21:55:08
回答 2查看 27.7K关注 0票数 16

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

到目前为止,我的情况如下:

代码语言:javascript
复制
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");
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-06 14:51:35

你可以用我的代码来做这个。对您的代码的一些解释:

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

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

代码语言:javascript
复制
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;
        }
    }
}

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

票数 48
EN

Stack Overflow用户

发布于 2020-09-03 10:31:38

由于我需要修改基于ACF字段的表单接收器,这里有一个基于@Brotheryura代码的复制和粘贴解决方案。

它允许您动态地修改电子邮件的收件人,而不需要在前端有任何隐藏的字段。Simpy将其放入模板functions.php中,并将$recipient = ...部件替换为您自己的函数或代码,以获得新的接收方。

代码语言:javascript
复制
add_action("wpcf7_before_send_mail", "wpcf7_change_recipient");
function wpcf7_change_recipient($WPCF7_ContactForm)
{
    $wpcf7      = WPCF7_ContactForm::get_current();
    $submission = WPCF7_Submission::get_instance();

    //some little magic to get the referers ID
    $recipient  = get_field('mail', url_to_postid(wp_get_referer()));

    if (!empty($recipient))
    {
        if ($submission)
        {
            $data = $submission->get_posted_data();

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

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

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

            // return current cf7 instance
            return $wpcf7;
        }
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25817442

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档