如何在联系人表单7中获取当前帖子ID wpcf7_BEVER_SEND_MAIL挂钩操作

时间:2019-12-16 作者:DesVal

我试图在发送之前处理CF7数据,并使用ACF函数更新当前帖子自定义字段,但我无法获取表单发送的当前帖子ID。我还尝试从全局$post变量获取ID,并获取\\u queryed\\u object\\u ID(),但也没有成功。

你知道我怎样才能得到发送表单的帖子的ID吗?

function dv_wpcf7_handle_form_data($wpcf7)
{
    $submission = WPCF7_Submission::get_instance();

    if ($submission) {
        $posted_data = $submission->get_posted_data();
    }

    // Check for ID of specific WPCF7 form
    if ($wpcf7->id() == 128) {
        $number_order = $posted_data[\'customer-number\'];
        $number_current_value = get_field(\'trip_available_seats\', get_the_ID()); // passing the ID to function doesn\'t work
        $number_new_value = $number_current_value - $number_order;

        if ($number_new_value >= 0) {
            update_field(\'trip_available_seats\', $number_new_value, get_the_ID());
        } else {
            $error = true;
            $err_msg = \'Error message...\';
        }
    }

    if (isset($error) && $error === true) {
        $msgs = $wpcf7->prop(\'messages\');
        $msgs[\'mail_sent_ok\'] = $err_msg;
        $wpcf7->set_properties(array(\'messages\' => $msgs));
        add_filter(\'wpcf7_skip_mail\', \'abort_mail_sending\');
    }

    return $wpcf7;
}
add_action(\'wpcf7_before_send_mail\', \'dv_wpcf7_handle_form_data\');

function abort_mail_sending($contact_form)
{
    return true;
}

4 个回复
最合适的回答,由SO网友:Pikamander2 整理而成

由于a backwards-incompatible change 在版本5.2中,您不能再使用get_posted_data().

相反,您可以使用id 返回的数组中的值get_current():

$contact_form = WPCF7_ContactForm::get_current();
$contact_form_id = $contact_form -> id;

SO网友:Awais

您可以从数组变量获取post ID$posted_data 表单发送自

  if ($submission) {
       $posted_data = $submission->get_posted_data();
       print_r($posted_data)
  }
如果你做了print_r 在上面你会看到这样的东西:

Array
(
    [_wpcf7] => 20
    [_wpcf7_version] => 5.1.6
    [_wpcf7_locale] => en_US
    [_wpcf7_unit_tag] => wpcf7-f20-p22-o1
    [_wpcf7_container_post] => 22  **//This is what you want.**
    [your-name] => Jon Doe
    [your-email] => [email protected]
    [your-subject] => subject
    [your-message] => message
)
编辑:

自CF7起version 5.2 获取;post"E;联系人表单绑定的ID为:

if ($submission) {
    print_r($_POST);
}
这将返回如下结果:

Array
(
    [_wpcf7] => 119 **//This is the "contact form" ID. But one should get that using WPCF7_ContactForm::get_current(); method **
    [_wpcf7_version] => 5.2.1
    [_wpcf7_locale] => en_US
    [_wpcf7_unit_tag] => wpcf7-f119-p120-o1
    [_wpcf7_container_post] => 120 **//This is the "post" ID**
    [_wpcf7_posted_data_hash] => 
    [your-name] => Jon
    [your-email] => Doe
    [your-subject] => Test
    [your-message] => Test
)

SO网友:DesVal

发现您可以使用$_POST[\'_wpcf7_container_post\'])

SO网友:matthias.wagner.wy

如果希望从wpcf7而不是$_POST 数组中,可以使用以下内容。这类似于如何检索发布的数据。仅使用get_meta 而不是get_posted_data:

$submission = WPCF7_Submission :: get_instance();
$submission->get_meta(\'container_post_id\');

相关推荐

如何覆盖/扩展WordPress函数IS_EMAIL

所以wordpress的核心并没有真正更新unicode域的电子邮件许可。即使是像德语umlauts(äÄöÖÜß)这样的简单特殊字符也不允许出现在名称的域部分。因此,我一直在考虑向is\\U电子邮件功能添加一个具有高优先级的过滤器,并简单地返回结果。但它似乎并没有像我预期的那样发挥作用。以下是我尝试过的:// Code written in my themes functions.php function kk_email_validation_override( $email ) {&