How to change data before sending in Contact form 7?

风格不统一 提交于 2019-12-06 14:11:23

问题


I have contact form on my website. And I need to change the value of one field before sending mail. For example name. I try like this:

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

add_action( 'wpcf7_before_send_mail', 'contactform7_before_send_mail' );

But in the email comes the value that is specified in the form.


回答1:


Recently faced the same problem.
There is a field in this form called "[s2-name]" for example. When a visitor submit out a form, I want to get this field, then change and send. After some information searching, I wrote this code:

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



回答2:


Try this:

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>


来源:https://stackoverflow.com/questions/32784063/how-to-change-data-before-sending-in-contact-form-7

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!