Last updated on October 18, 2023

WP Forms MailChimp integration

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

Connect WP Forms with MailChimp for emails.

To integrate WP Forms with MailChimp, you can use the wpsnippets_wpforms_mailchimp_integration() function. This function will add a new action hook that will be triggered when a form is submitted, allowing you to send the form data to MailChimp.

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

    // Get the form fields
    $form_fields = $form_data['fields'];

    // Get the form data
    $form_data = array();
    foreach ( $form_fields as $field ) {
        $field_id = $field['id'];
        $field_value = $fields[ $field_id ]['value'];
        $form_data[ $field_id ] = $field_value;
    }

    // Your MailChimp API key
    $api_key = 'YOUR_MAILCHIMP_API_KEY';

    // Your MailChimp list ID
    $list_id = 'YOUR_MAILCHIMP_LIST_ID';

    // Include the MailChimp API library
    require_once( 'path/to/mailchimp-api-library.php' );

    // Create a new instance of the MailChimp API
    $mailchimp = new MailChimp( $api_key );

    // Prepare the data to be sent to MailChimp
    $subscriber_data = array(
        'email_address' => $form_data['email'],
        'status' => 'subscribed',
        'merge_fields' => array(
            'FNAME' => $form_data['first_name'],
            'LNAME' => $form_data['last_name'],
        ),
    );

    // Subscribe the user to the MailChimp list
    $result = $mailchimp->post( "lists/$list_id/members", $subscriber_data );

    // Check if the subscription was successful
    if ( $result['status'] == 'subscribed' ) {
        // Subscription successful
    } else {
        // Subscription failed
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_mailchimp_integration', 10, 3 );

This code snippet adds a new action hook wpforms_process_complete that is triggered when a form is submitted. Inside the function wpsnippets_wpforms_mailchimp_integration(), we retrieve the form ID, form fields, and form data. Then, we include the MailChimp API library and create a new instance of the MailChimp API using your MailChimp API key. Next, we prepare the subscriber data to be sent to MailChimp, including the email address and merge fields. Finally, we use the MailChimp API to subscribe the user to the specified MailChimp list.

Remember to replace 'YOUR_MAILCHIMP_API_KEY' with your actual MailChimp API key and 'YOUR_MAILCHIMP_LIST_ID' with your actual MailChimp list ID.

Examples

Example 1: Adding a new subscriber to MailChimp when a form is submitted

This example demonstrates how to integrate WP Forms with MailChimp by adding a new subscriber to a MailChimp list when a form is submitted.

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

    // Get the submitted data
    $email = $fields['email'];
    $name = $fields['name'];

    // Prepare the data for MailChimp API
    $data = array(
        'email_address' => $email,
        'status' => 'subscribed',
        'merge_fields' => array(
            'FNAME' => $name,
        ),
    );

    // Convert the data to JSON
    $json_data = json_encode( $data );

    // Make a POST request to MailChimp API
    $response = wp_remote_post( 'https://<YOUR_MAILCHIMP_API_ENDPOINT>/lists/<YOUR_LIST_ID>/members', array(
        'headers' => array(
            'Authorization' => 'Basic ' . base64_encode( 'user:<YOUR_MAILCHIMP_API_KEY>' ),
            'Content-Type' => 'application/json',
        ),
        'body' => $json_data,
    ) );

    // Check if the request was successful
    if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) {
        // Subscriber added successfully
        // Do something here (e.g., display a success message)
    } else {
        // Failed to add subscriber
        // Do something here (e.g., display an error message)
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_mailchimp_integration', 10, 3 );

This code example hooks into the wpforms_process_complete action, which is triggered when a form is submitted. It retrieves the submitted form data, prepares the data in the required format for the MailChimp API, and makes a POST request to add a new subscriber to a MailChimp list. The response is then checked to determine if the subscriber was added successfully or not.

Example 2: Updating an existing subscriber in MailChimp when a form is submitted

This example demonstrates how to update an existing subscriber in MailChimp when a form is submitted, instead of adding a new subscriber.

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

    // Get the submitted data
    $email = $fields['email'];
    $name = $fields['name'];

    // Prepare the data for MailChimp API
    $data = array(
        'email_address' => $email,
        'status' => 'subscribed',
        'merge_fields' => array(
            'FNAME' => $name,
        ),
    );

    // Convert the data to JSON
    $json_data = json_encode( $data );

    // Make a PATCH request to MailChimp API
    $response = wp_remote_patch( 'https://<YOUR_MAILCHIMP_API_ENDPOINT>/lists/<YOUR_LIST_ID>/members/' . md5( strtolower( $email ) ), array(
        'headers' => array(
            'Authorization' => 'Basic ' . base64_encode( 'user:<YOUR_MAILCHIMP_API_KEY>' ),
            'Content-Type' => 'application/json',
        ),
        'body' => $json_data,
    ) );

    // Check if the request was successful
    if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) {
        // Subscriber updated successfully
        // Do something here (e.g., display a success message)
    } else {
        // Failed to update subscriber
        // Do something here (e.g., display an error message)
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_mailchimp_integration', 10, 3 );

This code example is similar to the previous one, but instead of making a POST request to add a new subscriber, it makes a PATCH request to update an existing subscriber in MailChimp. The subscriber is identified by their email address, which is used to generate the MD5 hash required for the API endpoint.

Example 3: Handling MailChimp API errors and displaying error messages

This example demonstrates how to handle MailChimp API errors and display custom error messages to the user when there is an issue with the integration.

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

    // Get the submitted data
    $email = $fields['email'];
    $name = $fields['name'];

    // Prepare the data for MailChimp API
    $data = array(
        'email_address' => $email,
        'status' => 'subscribed',
        'merge_fields' => array(
            'FNAME' => $name,
        ),
    );

    // Convert the data to JSON
    $json_data = json_encode( $data );

    // Make a POST request to MailChimp API
    $response = wp_remote_post( 'https://<YOUR_MAILCHIMP_API_ENDPOINT>/lists/<YOUR_LIST_ID>/members', array(
        'headers' => array(
            'Authorization' => 'Basic ' . base64_encode( 'user:<YOUR_MAILCHIMP_API_KEY>' ),
            'Content-Type' => 'application/json',
        ),
        'body' => $json_data,
    ) );

    // Check if the request was successful
    if ( ! is_wp_error( $response ) && wp_remote_retrieve_response_code( $response ) === 200 ) {
        // Subscriber added/updated successfully
        // Do something here (e.g., display a success message)
    } else {
        // Failed to add/update subscriber
        $error_message = wp_remote_retrieve_response_message( $response );
        // Do something here (e.g., display the error message to the user)
    }
}
add_action( 'wpforms_process_complete', 'wpsnippets_wpforms_mailchimp_integration', 10, 3 );

This code example handles MailChimp API errors by checking if the response is a WP_Error object or if the response code is not 200. If there is an error, the error message is retrieved from the response and can be used to display a custom error message to the user.

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

Leave a Reply

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