【问题标题】:How to change data before sending in Contact form 7?如何在联系表格 7 中发送之前更改数据?
【发布时间】:2015-12-23 09:32:09
【问题描述】:

我的网站上有联系表格。而且我需要在发送邮件之前更改一个字段的值。例如name。我尝试这样:

function contactform7_before_send_mail( $cf7 ) {
    $cf7->posted_data['your_name'] = 'John Doe';
}

add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );

但电子邮件中包含表单中指定的值。

【问题讨论】:

  • 你找到解决办法了吗?

标签: php wordpress contact-form-7


【解决方案1】:

试试这个:

post.php

    $_POST["your_name"] = "John Doe");
    do_shortcode("[cfdb-save-form-post]");

form.html

<form class="form-contact" action="post.php" method="post">
  <input type="text" name="your_name" />
</form>

【讨论】:

    【解决方案2】:

    最近遇到了同样的问题。
    例如,此表单中有一个名为“[s2-name]”的字段。当访问者提交表单时,我想获取此字段,然后更改并发送。经过一些信息搜索,我写了这段代码:

        add_action( 'wpcf7_before_send_mail', 'wpcf7_do_something_else_with_the_data', 90, 1 );
        
        function wpcf7_do_something_else_with_the_data( $WPCF7_ContactForm ){
        
            // Submission object, that generated when the user click the submit button.
            $submission = WPCF7_Submission :: get_instance();
        
            if ( $submission ){
                $posted_data = $submission->get_posted_data();      
                if ( empty( $posted_data ) ){ return; }
                
                // Got name data
                $name_data = $posted_data['s2-name'];
        
                // Do my code with this name
                $changed_name = 'something';
        
                // Got e-mail text
                $mail = $WPCF7_ContactForm->prop( 'mail' );
        
                // Replace "[s2-name]" field inside e-mail text
                $new_mail = str_replace( '[s2-name]', $changed_name, $mail );
        
                // Set
                $WPCF7_ContactForm->set_properties( array( 'mail' => $new_mail ) );
                
                return $WPCF7_ContactForm;
            }
        }
    

    测试:WP 5.4.2 和 Contact Form 7 版本 5.2

    【讨论】:

    • @Azamat 我已经用 CF7 Vers 测试了这段代码。 5.2.有用。仔细看看我的cmets。您是否从表单中正确输入了标签?你需要[your_tag_name]的[s2-name]在哪里,检查一下。
    • 对不起,我错了。我试图让它与第二封邮件一起工作,确认电子邮件。我发现,我必须在道具中有“mail_2”。您的代码适用于第一封(主要)电子邮件,在将道具更改为“mail_2”之后,它现在也适用于第二封邮件!谢谢!
    猜你喜欢
    • 2021-08-19
    • 1970-01-01
    • 2017-04-24
    • 2015-07-07
    • 2018-04-16
    • 2018-06-01
    • 2014-10-08
    • 2013-04-22
    • 2021-01-27
    相关资源
    最近更新 更多