WordPress Contact Form 7 如何分解日期表单

WordPress Contact Form 7 how to explode a date form

我有这个:

[date* SERVICE_DATE id:pickup class:field1]

是Contact Form 7,在邮件中是这样显示的:

Pick_up_Date: [SERVICE_DATE] - Year/Month/Day

...我收到了这封电子邮件:

Pick_up_Date: 2017-03-27 - Year/Month/Day

但是当我把它放在 PHP/HTML 网站上时,我能够分解日期并通过分解得到每个日期,就像这样:

        $pickup     = explode("/",$_POST['SERVICE_DATE']);

        Service_Day: '.$pickup[0].' 

        Service_Month: '.$pickup[1].' 

        Service_Year: '.$pickup[2].' 

如何使用 Contact Form 7 在 WordPress 中以爆炸式的方式完成这项工作?

您必须使用 Contact Form 7 中的预定义挂钩在发送前修改数据。考虑使用 before_send_mail 挂钩。

这是一个例子:

add_action( 'wpcf7_before_send_mail', 'wpcf7_modify_date' );

function wpcf7_modify_date($contact_form){

    // get current SUBMISSION instance
    $submission = WPCF7_Submission::get_instance();


    // Ok go forward
    if ($submission) {

        // get submission data
        $data = $submission->get_posted_data();

        // nothing's here... do nothing...
        if (empty($data))
            return;

        // get mail property
        $mail = $contact_form->prop( 'mail' ); // returns array with mail values

        /*
         * based on your current date display, you will explode by "-" rather than "/"
         * see: https://contactform7.com/date-field/ for information on how to change your date format
         *
         * NOTE: If you change your date format, you will have to change this function.
         */
        $exploded_date = explode("-", $data['SERVICE_DATE']);

        $year = $exploded_date[0];
        $month = $exploded_date[1];
        $day = $exploded_date[2];

        // Modify mail body
        $mail['body'] .= "Service_Day:" . $day . "\n";
        $mail['body'] .= "Service_Month:" . $month . "\n";
        $mail['body'] .= "Service_Year:" . $year . "\n";

        // set mail property with changed value(s)
        $contact_form->set_properties( array( 'mail' => $mail ) );

        return $contact_form;
    }
}

这个函数会在邮件发送前hook到提交的表单值,展开提交日期然后显示你想要的。您应该从 wp-admin 的 CF7 电子邮件设置中删除 Pick_up_Date: [SERVICE_DATE] - Year/Month/Day,因为这会自动将日期添加到电子邮件正文中。

编辑:要将日期放在电子邮件的开头,请执行以下操作:

// Assign the current mail body to a new variable
$mail_body = $mail['body'];

// Override the $mail['body'] with the new content
$mail['body'] = "Service_Day:" . $day . "\n";
$mail['body'] .= "Service_Month:" . $month . "\n";
$mail['body'] .= "Service_Year:" . $year . "\n";

// Append the original body below the new body
$mail['body'] .= $mail_body;