How to send Contact Form 7 booking details to WhatsApp in WordPress using PHP


Step 1: Set Up Contact Form 7

  1. Install Contact Form 7:
    • Go to Plugins > Add New in your WordPress dashboard.
    • Search for “Contact Form 7,” install, and activate it.
  2. Create a Booking Form:
    • Navigate to Contact > Add New in the WordPress admin.
    • Name your form (e.g., “Booking Form”).
    • Add fields for booking details. Example form code:html<label>Your Name (required) [text* your-name] </label> <label>Your Email (required) [email* your-email] </label> <label>Phone Number (required) [tel* your-phone] </label> <label>Booking Date [date booking-date] </label> <label>Message [textarea your-message] </label> [submit "Submit Booking"]
    • Save the form and copy the shortcode (e.g.,

      Error: Contact form not found.

      ).
  3. Add the Form to a Page:
    • Create a new page or edit an existing one.
    • Paste the shortcode and publish the page.

Also check Contact form 7 for OTP verification trough Whatsapp


Step 2: Choose a WhatsApp API Service

To send messages to WhatsApp, you’ll need a third-party API service since WhatsApp doesn’t provide a direct PHP integration. Popular options include:

  • Twilio
  • WhatsApp Business API
  • Third-party services like ChatAPI, WATI, or UltraMsg

For this tutorial, we’ll use Twilio because it’s reliable and beginner-friendly.

  1. Sign Up for Twilio:
    • Go to Twilio.com and create an account.
    • Complete the setup to get a Twilio Phone Number enabled for WhatsApp (you may need to contact Twilio support to enable WhatsApp for your number).
    • Note your Account SID, Auth Token, and WhatsApp-enabled Twilio Phone Number (e.g., whatsapp:+1234567890).

Step 3: Install Twilio PHP SDK

  1. Install Composer (if not already installed):
    • Follow instructions at getcomposer.org to install Composer on your server.
  2. Install Twilio SDK:
    • Navigate to your WordPress theme folder (e.g., /wp-content/themes/your-theme/).
    • Run the following command in your terminal:bashcomposer require twilio/sdk
    • This creates a vendor/ folder with the Twilio SDK.
  3. Include Composer Autoloader:
    • Open your theme’s functions.php file (e.g., /wp-content/themes/your-theme/functions.php).
    • Add the following at the top to load the Twilio SDK:phprequire_once get_template_directory() . '/vendor/autoload.php';

Step 4: Hook into Contact Form 7 Submission

Contact Form 7 provides a hook called wpcf7_mail_sent to execute code after a form is submitted successfully. We’ll use this to capture form data and send it to WhatsApp.

  1. Add Code to functions.php:
    • In your theme’s functions.php, add the following code:phpuse Twilio\Rest\Client; add_action('wpcf7_mail_sent', 'send_booking_to_whatsapp', 10, 1); function send_booking_to_whatsapp($contact_form) { // Replace with your form ID $form_id = 123; // Change to your Contact Form 7 form ID if ($contact_form->id() !== $form_id) { return; // Exit if not the target form } // Get form submission data $submission = WPCF7_Submission::get_instance(); if ($submission) { $posted_data = $submission->get_posted_data(); } // Extract form fields $name = isset($posted_data['your-name']) ? sanitize_text_field($posted_data['your-name']) : ''; $email = isset($posted_data['your-email']) ? sanitize_email($posted_data['your-email']) : ''; $phone = isset($posted_data['your-phone']) ? sanitize_text_field($posted_data['your-phone']) : ''; $booking_date = isset($posted_data['booking-date']) ? sanitize_text_field($posted_data['booking-date']) : ''; $message = isset($posted_data['your-message']) ? sanitize_textarea_field($posted_data['your-message']) : ''; // Format the WhatsApp message $whatsapp_message = "New Booking:\n"; $whatsapp_message .= "Name: $name\n"; $whatsapp_message .= "Email: $email\n"; $whatsapp_message .= "Phone: $phone\n"; $whatsapp_message .= "Booking Date: $booking_date\n"; $whatsapp_message .= "Message: $message"; // Twilio credentials $twilio_sid = 'YOUR_TWILIO_ACCOUNT_SID'; // Replace with your Twilio Account SID $twilio_token = 'YOUR_TWILIO_AUTH_TOKEN'; // Replace with your Twilio Auth Token $twilio_whatsapp_number = 'whatsapp:+1234567890'; // Your Twilio WhatsApp number $recipient_whatsapp = 'whatsapp:+YOUR_PHONE_NUMBER'; // Replace with the recipient's WhatsApp number (e.g., your business number) try { // Initialize Twilio client $twilio = new Client($twilio_sid, $twilio_token); // Send WhatsApp message $twilio->messages->create( $recipient_whatsapp, [ 'from' => $twilio_whatsapp_number, 'body' => $whatsapp_message ] ); } catch (Exception $e) { // Log error (optional) error_log('Twilio WhatsApp Error: ' . $e->getMessage()); } }
  2. Update the Code:
    • Replace 123 with your actual Contact Form 7 form ID (found in the Contact Form 7 admin panel).
    • Replace YOUR_TWILIO_ACCOUNT_SID and YOUR_TWILIO_AUTH_TOKEN with your Twilio credentials.
    • Replace whatsapp:+1234567890 with your Twilio WhatsApp-enabled number.
    • Replace whatsapp:+YOUR_PHONE_NUMBER with the recipient’s WhatsApp number (e.g., your business WhatsApp number).

Step 5: Test the Form

  1. Submit a Test Booking:
    • Go to the page with your Contact Form 7 form.
    • Fill out and submit the form.
  2. Check WhatsApp:
    • Verify that the recipient’s WhatsApp number receives a message with the booking details.
  3. Debug if Needed:
    • If the message doesn’t arrive, check your WordPress error logs (wp-content/debug.log if debugging is enabled).
    • Ensure your Twilio credentials and phone numbers are correct.
    • Confirm that the Twilio number is WhatsApp-enabled.

Step 6: Secure Your Credentials

Storing sensitive data like Twilio credentials directly in functions.php isn’t secure. Instead:

  1. Use a Config File:
    • Create a file (e.g., twilio-config.php) in your theme folder:php<?php define('TWILIO_SID', 'YOUR_TWILIO_ACCOUNT_SID'); define('TWILIO_TOKEN', 'YOUR_TWILIO_AUTH_TOKEN'); define('TWILIO_WHATSAPP_NUMBER', 'whatsapp:+1234567890'); define('RECIPIENT_WHATSAPP', 'whatsapp:+YOUR_PHONE_NUMBER');
    • Include it in functions.php:phprequire_once get_template_directory() . '/twilio-config.php';
  2. Update the Twilio Code:
    • Replace the hardcoded credentials in the send_booking_to_whatsapp function:php$twilio_sid = TWILIO_SID; $twilio_token = TWILIO_TOKEN; $twilio_whatsapp_number = TWILIO_WHATSAPP_NUMBER; $recipient_whatsapp = RECIPIENT_WHATSAPP;
  3. Secure the Config File:
    • Add twilio-config.php to your .gitignore if using version control.
    • Restrict access by adding to your .htaccess:apache<Files "twilio-config.php"> Order Allow,Deny Deny from all </Files>

Step 7: Optional Enhancements

  • Validate Phone Numbers:
    • Add validation in Contact Form 7 to ensure the your-phone field is a valid WhatsApp number.
    • Use the wpcf7_validate_tel* filter if needed.
  • Customize the Message:
    • Modify the $whatsapp_message string to include additional details or formatting.
  • Send to Multiple Recipients:
    • Loop through an array of recipient numbers to send the message to multiple WhatsApp numbers:php$recipients = ['whatsapp:+NUMBER1', 'whatsapp:+NUMBER2']; foreach ($recipients as $recipient_whatsapp) { $twilio->messages->create($recipient_whatsapp, [ 'from' => $twilio_whatsapp_number, 'body' => $whatsapp_message ]); }
  • Add a Confirmation:
    • Notify the user that their booking was sent to WhatsApp by customizing the form’s success message.

Troubleshooting Tips

  • Form Not Sending:
    • Ensure the form ID in the code matches your Contact Form 7 form ID.
    • Check for JavaScript conflicts on the page (use browser console).
  • WhatsApp Message Not Received:
    • Verify Twilio credentials and WhatsApp-enabled number.
    • Check Twilio’s debug logs in your Twilio dashboard.
    • Ensure the recipient number is active on WhatsApp.
  • PHP Errors:
    • Enable WordPress debugging (define(‘WP_DEBUG’, true); in wp-config.php).
    • Check error logs for Twilio SDK issues.

Notes

  • Twilio Costs: Twilio charges for WhatsApp messages. Monitor usage in your Twilio dashboard.
  • Alternative APIs: If Twilio isn’t suitable, explore other services like UltraMsg or WATI, but their setup will differ.
  • Compliance: Ensure compliance with WhatsApp’s policies and data privacy laws (e.g., GDPR, CCPA) when handling user data.

By following these steps, your Contact Form 7 booking details will be sent to WhatsApp automatically after form submission. Let me know if you need help with specific parts!Here’s a step-by-step tutorial on how to send Contact Form 7 booking details to WhatsApp in WordPress using PHP. This guide assumes you have a WordPress site with Contact Form 7 installed and a basic understanding of WordPress development.


Step 1: Set Up Contact Form 7

  1. Install Contact Form 7:
    • Go to Plugins > Add New in your WordPress dashboard.
    • Search for “Contact Form 7,” install, and activate it.
  2. Create a Booking Form:
    • Navigate to Contact > Add New in the WordPress admin.
    • Name your form (e.g., “Booking Form”).
    • Add fields for booking details. Example form code:html<label>Your Name (required) [text* your-name] </label> <label>Your Email (required) [email* your-email] </label> <label>Phone Number (required) [tel* your-phone] </label> <label>Booking Date [date booking-date] </label> <label>Message [textarea your-message] </label> [submit "Submit Booking"]
    • Save the form and copy the shortcode (e.g.,

      Error: Contact form not found.

      ).
  3. Add the Form to a Page:
    • Create a new page or edit an existing one.
    • Paste the shortcode and publish the page.

Step 2: Choose a WhatsApp API Service

To send messages to WhatsApp, you’ll need a third-party API service since WhatsApp doesn’t provide a direct PHP integration. Popular options include:

  • Twilio
  • WhatsApp Business API
  • Third-party services like ChatAPI, WATI, or UltraMsg

For this tutorial, we’ll use Twilio because it’s reliable and beginner-friendly.

  1. Sign Up for Twilio:
    • Go to Twilio.com and create an account.
    • Complete the setup to get a Twilio Phone Number enabled for WhatsApp (you may need to contact Twilio support to enable WhatsApp for your number).
    • Note your Account SID, Auth Token, and WhatsApp-enabled Twilio Phone Number (e.g., whatsapp:+1234567890).

Step 3: Install Twilio PHP SDK

  1. Install Composer (if not already installed):
    • Follow instructions at getcomposer.org to install Composer on your server.
  2. Install Twilio SDK:
    • Navigate to your WordPress theme folder (e.g., /wp-content/themes/your-theme/).
    • Run the following command in your terminal:bashcomposer require twilio/sdk
    • This creates a vendor/ folder with the Twilio SDK.
  3. Include Composer Autoloader:
    • Open your theme’s functions.php file (e.g., /wp-content/themes/your-theme/functions.php).
    • Add the following at the top to load the Twilio SDK:phprequire_once get_template_directory() . '/vendor/autoload.php';

Step 4: Hook into Contact Form 7 Submission

Contact Form 7 provides a hook called wpcf7_mail_sent to execute code after a form is submitted successfully. We’ll use this to capture form data and send it to WhatsApp.

  1. Add Code to functions.php:
    • In your theme’s functions.php, add the following code:phpuse Twilio\Rest\Client; add_action('wpcf7_mail_sent', 'send_booking_to_whatsapp', 10, 1); function send_booking_to_whatsapp($contact_form) { // Replace with your form ID $form_id = 123; // Change to your Contact Form 7 form ID if ($contact_form->id() !== $form_id) { return; // Exit if not the target form } // Get form submission data $submission = WPCF7_Submission::get_instance(); if ($submission) { $posted_data = $submission->get_posted_data(); } // Extract form fields $name = isset($posted_data['your-name']) ? sanitize_text_field($posted_data['your-name']) : ''; $email = isset($posted_data['your-email']) ? sanitize_email($posted_data['your-email']) : ''; $phone = isset($posted_data['your-phone']) ? sanitize_text_field($posted_data['your-phone']) : ''; $booking_date = isset($posted_data['booking-date']) ? sanitize_text_field($posted_data['booking-date']) : ''; $message = isset($posted_data['your-message']) ? sanitize_textarea_field($posted_data['your-message']) : ''; // Format the WhatsApp message $whatsapp_message = "New Booking:\n"; $whatsapp_message .= "Name: $name\n"; $whatsapp_message .= "Email: $email\n"; $whatsapp_message .= "Phone: $phone\n"; $whatsapp_message .= "Booking Date: $booking_date\n"; $whatsapp_message .= "Message: $message"; // Twilio credentials $twilio_sid = 'YOUR_TWILIO_ACCOUNT_SID'; // Replace with your Twilio Account SID $twilio_token = 'YOUR_TWILIO_AUTH_TOKEN'; // Replace with your Twilio Auth Token $twilio_whatsapp_number = 'whatsapp:+1234567890'; // Your Twilio WhatsApp number $recipient_whatsapp = 'whatsapp:+YOUR_PHONE_NUMBER'; // Replace with the recipient's WhatsApp number (e.g., your business number) try { // Initialize Twilio client $twilio = new Client($twilio_sid, $twilio_token); // Send WhatsApp message $twilio->messages->create( $recipient_whatsapp, [ 'from' => $twilio_whatsapp_number, 'body' => $whatsapp_message ] ); } catch (Exception $e) { // Log error (optional) error_log('Twilio WhatsApp Error: ' . $e->getMessage()); } }
  2. Update the Code:
    • Replace 123 with your actual Contact Form 7 form ID (found in the Contact Form 7 admin panel).
    • Replace YOUR_TWILIO_ACCOUNT_SID and YOUR_TWILIO_AUTH_TOKEN with your Twilio credentials.
    • Replace whatsapp:+1234567890 with your Twilio WhatsApp-enabled number.
    • Replace whatsapp:+YOUR_PHONE_NUMBER with the recipient’s WhatsApp number (e.g., your business WhatsApp number).

Step 5: Test the Form

  1. Submit a Test Booking:
    • Go to the page with your Contact Form 7 form.
    • Fill out and submit the form.
  2. Check WhatsApp:
    • Verify that the recipient’s WhatsApp number receives a message with the booking details.
  3. Debug if Needed:
    • If the message doesn’t arrive, check your WordPress error logs (wp-content/debug.log if debugging is enabled).
    • Ensure your Twilio credentials and phone numbers are correct.
    • Confirm that the Twilio number is WhatsApp-enabled.

Step 6: Secure Your Credentials

Storing sensitive data like Twilio credentials directly in functions.php isn’t secure. Instead:

  1. Use a Config File:
    • Create a file (e.g., twilio-config.php) in your theme folder:php<?php define('TWILIO_SID', 'YOUR_TWILIO_ACCOUNT_SID'); define('TWILIO_TOKEN', 'YOUR_TWILIO_AUTH_TOKEN'); define('TWILIO_WHATSAPP_NUMBER', 'whatsapp:+1234567890'); define('RECIPIENT_WHATSAPP', 'whatsapp:+YOUR_PHONE_NUMBER');
    • Include it in functions.php:phprequire_once get_template_directory() . '/twilio-config.php';
  2. Update the Twilio Code:
    • Replace the hardcoded credentials in the send_booking_to_whatsapp function:php$twilio_sid = TWILIO_SID; $twilio_token = TWILIO_TOKEN; $twilio_whatsapp_number = TWILIO_WHATSAPP_NUMBER; $recipient_whatsapp = RECIPIENT_WHATSAPP;
  3. Secure the Config File:
    • Add twilio-config.php to your .gitignore if using version control.
    • Restrict access by adding to your .htaccess:apache<Files "twilio-config.php"> Order Allow,Deny Deny from all </Files>

Step 7: Optional Enhancements

  • Validate Phone Numbers:
    • Add validation in Contact Form 7 to ensure the your-phone field is a valid WhatsApp number.
    • Use the wpcf7_validate_tel* filter if needed.
  • Customize the Message:
    • Modify the $whatsapp_message string to include additional details or formatting.
  • Send to Multiple Recipients:
    • Loop through an array of recipient numbers to send the message to multiple WhatsApp numbers:php$recipients = ['whatsapp:+NUMBER1', 'whatsapp:+NUMBER2']; foreach ($recipients as $recipient_whatsapp) { $twilio->messages->create($recipient_whatsapp, [ 'from' => $twilio_whatsapp_number, 'body' => $whatsapp_message ]); }
  • Add a Confirmation:
    • Notify the user that their booking was sent to WhatsApp by customizing the form’s success message.

Troubleshooting Tips

  • Form Not Sending:
    • Ensure the form ID in the code matches your Contact Form 7 form ID.
    • Check for JavaScript conflicts on the page (use browser console).
  • WhatsApp Message Not Received:
    • Verify Twilio credentials and WhatsApp-enabled number.
    • Check Twilio’s debug logs in your Twilio dashboard.
    • Ensure the recipient number is active on WhatsApp.
  • PHP Errors:
    • Enable WordPress debugging (define(‘WP_DEBUG’, true); in wp-config.php).
    • Check error logs for Twilio SDK issues.

Notes

  • Twilio Costs: Twilio charges for WhatsApp messages. Monitor usage in your Twilio dashboard.
  • Alternative APIs: If Twilio isn’t suitable, explore other services like UltraMsg or WATI, but their setup will differ.
  • Compliance: Ensure compliance with WhatsApp’s policies and data privacy laws (e.g., GDPR, CCPA) when handling user data.

By following these steps, your Contact Form 7 booking details will be sent to WhatsApp automatically after form submission. Let me know if you need help with specific parts!

About admin

Online Web Service Provider : Search Engine Optimization, Websites, Digital Marketing, Domain, Hosting, Emails, SSL and more

View all posts by admin →

Leave a Reply

Your email address will not be published. Required fields are marked *