Tiếng Việt

WordPress: Contact Form 7 API

cat.png

Author

Phước Hữu

02/26/2023

Share

WordPress-logotype-standard.png

WordPress: Contact Form 7 API

1. An introduction about Contact Form 7

Contact Form 7 is one of the most popular plugins in the WordPress community used to create and manage contact forms. With it, users can easily create diverse and customized contact forms according to their needs without having to use any single line of code. Besides, Contact Form 7 also provides many useful features to help users manage and handle contact forms conveniently and efficiently.

contact form 7.png

2. Example of submitting a Contact Form 7 form using the API

I prepared a form like this:

 

To demonstrate how to submit a form in Contact Form 7 using the API, I've created a sample form with fields including 'your-name,' 'your-email,' 'your-subject,' and 'your-message.' Contact Form 7 provides a link for submitting forms through the API, such as http://demo.local/wp-json/contact-form-7/v1/contact-forms/171/feedback, where '171' is the form ID. To test this, I used Postman to send an email:

When you use Contact Form 7 to send an email, the response structure that is normally returned includes the following properties:

{     
  "contact_form_id": 171,     
  "status": "mail_sent",     
  "message": "Thank you for your message. It has been sent.",     
  "posted_data_hash": "286ba8559707c76ee30f82f97b09bc32",     
  "into": "#",     
  "invalid_fields": [] 
}
  • contact_form_id: The ID of the Contact Form 7 form that was submitted.
  • status: The status of the form submission. In this example, the value is "mail_sent", indicating that the email was sent successfully.
  • message: A message indicating the status of the form submission. In this example, the value is "Thank you for your message. It has been sent.", which is the default success message for Contact Form 7.
  • posted_data_hash: A hash of the form data that was submitted. This can be used to verify the integrity of the data.
  • into: The form input field or fields that the form data was sent to. In this example, the value is "#", indicating that the data was not sent to a specific email address but rather to the default email address specified in the Contact Form 7 settings.
  • invalid_fields: An array of input fields that failed validation. In this example, the value is an empty array [], indicating that all fields passed validation.

3. Customizing the response generated by Contact Form 7

First we must know the state of a form after it is submitted so that we can customize it to suit your needs based on that:
Contact Form 7 has the following status values that may be returned in the response:

  • mail_sent: The email has been sent successfully.
  • mail_failed: The email failed to send.
  • validation_failed: The form data failed validation.
  • spam: The form submission was identified as spam.
  • mail_sent_akismet: The email was sent successfully, but it was flagged as spam by Akismet.
  • mail_sent_ng: The email was sent successfully, but it failed to deliver due to an error in the email server configuration or other reasons.

These status values can be used to customize the response message that is returned to the user when the form is submitted. You can use the wpcf7_ajax_json_echo hook to customize the response message based on the specific status value that is returned.

<?php
add_action('wpcf7_ajax_json_echo', 'custom_response_message', 10, 2);

function custom_response_message($response, $result)
{
    if ($response['status'] === 'mail_sent') {
        $response['meta']   = (object) array(
            'id'              => $response['contact_form_id'],
            'invalidFields'   => $response['invalid_fields'],
            'postedDataHash'  => $response['posted_data_hash'],
            'into'            => $response['into']
        );
        $response['message'] = $response['message'];
        $response['error']  = null;
        $response['code']   = 204;
}

    unset($response['contact_form_id']);
    unset($response['message']);
    unset($response['invalid_fields']);
    unset($response['posted_data_hash']);
    unset($response['into']);
    return $response;
}

Let me clarify the above code's purpose: it aims to handle the response when a mail is sent successfully. The code creates a "meta" section by combining relevant fields such as contact_form_id, invalid_fields, posted_data_hash, and then removes them from the original response. This approach is useful when you want to customize the response and omit certain details that are not relevant to your use case.


I hope that the above example has given you a better understanding of how to work with Contact Form 7's API. By modifying the response data, you can customize the message that is returned to the user after submitting a form. Keep in mind that this is just one example of how to use the API and there are many other ways to work with it.

If you'd like to learn more, here are some references that you might find useful:
Contact Form 7
ChatGPT

 

 

Share

Contact Us