Last updated on October 18, 2023

WP Forms email marketing integration

Don’t know where to add this snippet? Read our guide: How to add code snippets.

Integrate email marketing with WP Forms.

To integrate WP Forms with an email marketing service, you can use the wpforms_process_complete action hook along with the wpsnippets_wpforms_integration function. This code snippet allows you to send form data to an email marketing service when a WP Forms submission is completed.

/**
 * Send WP Forms data to email marketing service.
 */
function wpsnippets_wpforms_integration( $fields, $entry, $form_data ) {
    // Get form data
    $form_id = $form_data['id'];
    $form_title = $form_data['title'];

    // Get form fields
    $name = $fields['name'];
    $email = $fields['email'];
    $message = $fields['message'];

    // Prepare data to send to email marketing service
    $data = array(
        'form_id' => $form_id,
        'form_title' => $form_title,
        'name' => $name,
        'email' => $email,
        'message' => $message,
    );

    // Send data to email marketing service
    // Replace 'email_marketing_service_function' with the actual function to send data to your email marketing service
    email_marketing_service_function( $data );
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_integration', 10, 3 );

This code snippet hooks into the wpforms_process_complete action, which is triggered when a WP Forms submission is completed. It retrieves the form data, including the form ID and title, as well as the submitted fields such as name, email, and message. It then prepares the data to be sent to the email marketing service by creating an array with the necessary information. Finally, it calls the email_marketing_service_function (replace with the actual function) to send the data to the email marketing service.

Note: You need to replace 'email_marketing_service_function' with the actual function or code that sends the data to your email marketing service.

Examples

Example 1: Integrating WP Forms with Mailchimp

This example demonstrates how to integrate WP Forms with Mailchimp to automatically add form submissions to a Mailchimp email list.

function wpsnippets_wpforms_mailchimp_integration( $fields, $entry, $form_data ) {
    // Get the form ID
    $form_id = $form_data['id'];

    // Check if the form ID matches the desired form
    if ( $form_id == 123 ) {
        // Get the submitted email address
        $email = $fields['email'];

        // Add the email to the Mailchimp list
        $result = wpsnippets_mailchimp_subscribe( $email );

        // Check if the subscription was successful
        if ( $result ) {
            // Log the successful subscription
            wpsnippets_log( 'Email subscribed: ' . $email );
        } else {
            // Log the failed subscription
            wpsnippets_log( 'Failed to subscribe email: ' . $email );
        }
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_mailchimp_integration', 10, 3 );

In this code example, we use the wpforms_process_complete action hook to trigger the integration when a WP Forms submission is completed. We check if the submitted form ID matches the desired form, extract the email address from the form fields, and then call the wpsnippets_mailchimp_subscribe function to add the email to a Mailchimp list. We log the result of the subscription using the wpsnippets_log function.

Example 2: Integrating WP Forms with Constant Contact

This example demonstrates how to integrate WP Forms with Constant Contact to automatically add form submissions to a Constant Contact email list.

function wpsnippets_wpforms_constant_contact_integration( $fields, $entry, $form_data ) {
    // Get the form ID
    $form_id = $form_data['id'];

    // Check if the form ID matches the desired form
    if ( $form_id == 456 ) {
        // Get the submitted email address
        $email = $fields['email'];

        // Add the email to the Constant Contact list
        $result = wpsnippets_constant_contact_subscribe( $email );

        // Check if the subscription was successful
        if ( $result ) {
            // Log the successful subscription
            wpsnippets_log( 'Email subscribed: ' . $email );
        } else {
            // Log the failed subscription
            wpsnippets_log( 'Failed to subscribe email: ' . $email );
        }
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_constant_contact_integration', 10, 3 );

In this code example, we use the wpforms_process_complete action hook to trigger the integration when a WP Forms submission is completed. We check if the submitted form ID matches the desired form, extract the email address from the form fields, and then call the wpsnippets_constant_contact_subscribe function to add the email to a Constant Contact list. We log the result of the subscription using the wpsnippets_log function.

Example 3: Integrating WP Forms with Sendinblue

This example demonstrates how to integrate WP Forms with Sendinblue to automatically add form submissions to a Sendinblue email list.

function wpsnippets_wpforms_sendinblue_integration( $fields, $entry, $form_data ) {
    // Get the form ID
    $form_id = $form_data['id'];

    // Check if the form ID matches the desired form
    if ( $form_id == 789 ) {
        // Get the submitted email address
        $email = $fields['email'];

        // Add the email to the Sendinblue list
        $result = wpsnippets_sendinblue_subscribe( $email );

        // Check if the subscription was successful
        if ( $result ) {
            // Log the successful subscription
            wpsnippets_log( 'Email subscribed: ' . $email );
        } else {
            // Log the failed subscription
            wpsnippets_log( 'Failed to subscribe email: ' . $email );
        }
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_sendinblue_integration', 10, 3 );

In this code example, we use the wpforms_process_complete action hook to trigger the integration when a WP Forms submission is completed. We check if the submitted form ID matches the desired form, extract the email address from the form fields, and then call the wpsnippets_sendinblue_subscribe function to add the email to a Sendinblue list. We log the result of the subscription using the wpsnippets_log function.

Last updated on October 18, 2023. Originally posted on November 13, 2023.

Leave a Reply