【问题标题】:How do I change the recipient in functions.php in WordPress Contact Form 7?如何在 WordPress 联系表 7 中的 functions.php 中更改收件人?
【发布时间】:2021-08-18 08:04:44
【问题描述】:

我正在尝试用高级自定义字段插件中的值替换联系人表单 7 插件中的电子邮件收件人,因为我在每条记录中都有一个动态收件人。我在网上找到了一个函数,但它对我不起作用,表单返回发送错误。

function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
            global $post;
            $dynamic_email = get_field('form_email',$post->ID); // get your email address...
        
            $properties = $contact_form->get_properties();
            $properties['mail']['recipient'] = $dynamic_email;
            $contact_form->set_properties($properties);
        
            return $contact_form;
        
        }
add_filter( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );

也许有一个更简单的函数可以工作,或者我做错了什么?

【问题讨论】:

    标签: wordpress contact-form-7


    【解决方案1】:

    您的函数有几处需要更新。

    1. 这是一个操作,而不是过滤器add_action,因此您不必返回任何内容。
    2. 无法按照您的方式检索 post_id。使用 $submission 类中的属性。
    function wpcf7_before_send_mail_function( $contact_form, $abort, $submission ) {
        // Container Post ID from CF7 $submission class
        // $submission->get_meta('container_post_id')
        $dynamic_email = get_field('form_email', $submission->get_meta('container_post_id'));
    
        $properties = $contact_form->get_properties();
        $properties['mail']['recipient'] = $dynamic_email;
        $contact_form->set_properties($properties);
        // You don't have to return $contact_form, you're using the set_properties function.
        //return $contact_form;
    }
    add_action( 'wpcf7_before_send_mail', 'wpcf7_before_send_mail_function', 10, 3 );
    

    【讨论】:

      猜你喜欢
      • 2022-10-22
      • 1970-01-01
      • 2018-02-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-25
      • 1970-01-01
      • 2016-09-25
      相关资源
      最近更新 更多