在联系表7中发送之前如何更改数据? [英] How to change data before sending in Contact form 7?

查看:44
本文介绍了在联系表7中发送之前如何更改数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

推荐答案

最近遇到了同样的问题。

该表单中有一个名为 [s2-name]的字段,例如。当访客提交表单时,我想获取此字段,然后进行更改并发送。搜索了一些信息后,我编写了以下代码:

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

经过以下测试:WP 5.4.2和Contact Form 7版本5.2

Tested with: WP 5.4.2 and Contact Form 7 Version 5.2

这篇关于在联系表7中发送之前如何更改数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆