动态更改 CF7 表单值

2022-08-31 00:09:14

我一直在尝试动态更改CF7表单字段,而无需使用联系表单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

您可以使用我的代码来执行此操作。代码的一些说明:

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

2) 无需使用回调和 .用于此 .&$cf7$submissionreturn

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;
        }
    }
}

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


答案 2

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

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

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;
        }
    }
}

推荐