Integrating WPForms with SendSquared: A Technical Overview

The following code hooks into the wpforms_process_complete action in WPForms, 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-field-id and your-name-field-id with the actual field IDs 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.

add_action( 'wpforms_process_complete', 'custom_form_submission', 10, 4 );
function custom_form_submission( $fields, $entry, $form_data, $entry_id ) {
  // Get the form data
  $email = $fields['your-email-field-id']['value'];
  $name = $fields['your-name-field-id']['value'];

  // 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 ) );
  }
}

If you have any additional questions, feel free to reach out to SendSquared support.

support@sendsquared.com

Leave a Reply

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