Integration Contact Form 7 with SendSquared: A Technical Overview

The following code hooks into the `wpcf7_mail_sent` action in Contact Form 7, which is triggered after a form is submitted. It collects the form data, sets up the API data, and uses `wp_remote_post` to send the data to the API. The code also checks the response code to ensure that the data was successfully sent to the API. If there is an error, it logs the error.

Note: You will need to replace your-email and your-name with the form field names for the email and name fields, respectively, and your-list-group-uuid with the actual UUID of the list group the user will subscribe to.

<?php 

add_action( 'wpcf7_mail_sent', 'custom_form_submission' );
function custom_form_submission( $contact_form ) {
  // Get the form data
  $submission = WPCF7_Submission::get_instance();
  if ( $submission ) {
    $posted_data = $submission->get_posted_data();
    $email = $posted_data['your-email'];
    $name = $posted_data['your-name'];
  }

  // Set the API URL with the token
  $token = "your-list-group-uuid";
  $api_url = "https://app-api.sendsquared.com/v1/pub/popup?token=$token";

  // Set up the API data
  $api_data = array(
    "email" => $email,
    "name" => $name,
  );

  // Use wp_remote_post to send the data to the API
  $response = wp_remote_post( $api_url, array(
    "body" => $api_data
  ) );

  // Check the response code
  if ( is_wp_error( $response ) || wp_remote_retrieve_response_code( $response ) != 200 ) {
    // Log an error
    error_log( "Error submitting form: " . print_r( $response, true ) );
  }
}

Leave a Reply

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