📜  发送 cf7 事件 (1)

📅  最后修改于: 2023-12-03 15:07:25.551000             🧑  作者: Mango

发送 CF7 事件

CF7(Contact Form 7)是一个WordPress插件,它允许网站管理员在其网站上轻松创建联系表单。如果您是WordPress开发人员并且正在开发使用CF7插件的WordPress网站,您可能需要发送CF7事件以与其他应用程序(例如CRM系统)集成。本文将介绍如何在WordPress中发送CF7事件。

1. 使用 WPCF7_Submission 类

要在WordPress中发送CF7事件,您需要使用 WPCF7_Submission 类。这个类的作用是将联系表单的所有数据收集为一个数组并将其发送到指定的URL。以下是一个发送CF7事件的示例代码:

function send_cf7_event() {
    // Get the submission object from the global variable.
    $submission = WPCF7_Submission::get_instance();
    
    // Get the contact form ID.
    $contact_form_id = $submission->get_contact_form_id();
    
    // Get the form data.
    $form_data = $submission->get_posted_data();
    
    // Create the request data.
    $request_data = array(
        'form_id' => $contact_form_id,
        'form_data' => $form_data
    );
    
    // Send the request.
    $response = wp_remote_post('https://example.com/event', array(
        'body' => json_encode($request_data),
        'headers' => array( 'Content-Type' => 'application/json' ),
        'timeout' => '5',
        'sslverify' => false // Do not verify SSL certificate.
    ));
    
    if (is_wp_error($response)) {
        // Handle the error.
    } else {
        // Handle the response.
    }
}

在上面的代码中,我们通过调用 WPCF7_Submission::get_instance() 获取提交对象,并使用 WPCF7_Submission 类的其他方法从对象中获取联系表单数据。我们然后创建一个请求数据数组,然后使用 WordPress 自带的 wp_remote_post() 函数发送POST请求。

2. 将发送事件添加到 CF7 邮件成功后的钩子中

一旦您已经编写了发送CF7事件的函数,您需要将其添加到CF7的邮箱钩子中。以下是一个例子:

add_action('wpcf7_mail_sent', 'send_cf7_event');

这将使您的发送函数在每次成功发送CF7邮件时运行。

总结

以上就是在 WordPress 中发送 CF7 事件的介绍。通过使用 WPCF7_Submission 类,您可以轻松地将联系表单数据发送到其他应用程序或服务。