在提交 Elementor 表单时通过 WordPress function.php 文件动态添加 CC 或 BCC 电子邮件地址

问题描述 投票:0回答:1

我目前正在尝试通过 WordPress function.php 文件将 CC 电子邮件地址动态添加到 Elementor 表单,但我似乎不知道如何实现。我的客户希望我仅在选择下拉/选择字段中的特定值时添加抄送电子邮件地址。不适用于整个表格。

我能够在添加抄送电子邮件地址时对逻辑进行编程,但我无法弄清楚应该使用哪个 Elementor Hook 以及如何指示它添加抄送电子邮件。非常感谢任何帮助。

目前我没有我尝试过的代码示例,因为没有任何效果。我仍在尝试弄清楚是否可以完成,如果可以,我需要如何去做。

php wordpress elementor
1个回答
0
投票

通过仔细查看 Elementor Forms API 文档,我使用了 elementor_pro/forms/processelementor_pro/forms/wp_mail_headers 钩子,并设法让它工作。

这就是我所做的:

##############################################################################
// Action is called when the forms is submitted and being processed
##############################################################################
add_action( 'elementor_pro/forms/process', 'add_cc_emails_to_submitted_elementor_forms', 10, 2 );
function add_cc_emails_to_submitted_elementor_forms( $record, $handler ) {
    // Get the form name
    $form_name = $record->get_form_settings( 'form_name' );
    
    // Check which form is being processed
    if ( $form_name !== 'FORM NAME') {
        return;
    }

    // Create an array of all the fields used in the form
    $raw_fields = $record->get( 'fields' );
    
    // Get the selected drop down value
    $drop_down_field_value = $raw_fields['field_name']['value'];
    
    // Set the CC email addresses
    if( $drop_down_field_value === 'SOMETHING' ){   
        // Call the filter to apply the needed CC Emails
        add_filter( 'elementor_pro/forms/wp_mail_headers', 'add_cc_emails_to_elementor_form', 10, 1 );
    }
}

##############################################################################
// Add CC email addresses to the Email Header
##############################################################################
function add_cc_emails_to_elementor_form( $headers ) {
    
    // Set all the CC email addresses   
    $headers .= "Cc: [email protected]\r\n";
    $headers .= "Cc: [email protected]\r\n";
    
    return $headers;
}
© www.soinside.com 2019 - 2024. All rights reserved.