如何在发送联系表格7之前更改数据?

时间:2015-09-25 14:07:29

标签: php wordpress

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

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

add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );

但是在电子邮件中提供了表单中指定的值。

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:1)

最近遇到了同样的问题。
例如,这种形式的字段称为“ [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;
        }
    }