MailChimp

The Quform WordPress v2 documentation is not yet finished. Please post on the forums if you have any questions in the mean time.

Click here to visit the complete documentation for Quform 1.x

This guide will show you how to add a new subscriber to MailChimp when a user submits a Quform form. In this guide we’ll create a new plugin to contain the code needed for the integration to work.

Step 1 – create a WordPress plugin

Create a new folder named quform-mailchimp and inside it create a file named quform-mailchimp.php

Inside the quform-mailchimp.php file add the following code.

1
2
3
4
5
6
7
8
910
11
12
13
14151617
18
19
2021
22
23
24
25
26
27
28
29
30
31
32
33
34
<?php
 
/*
 * Plugin Name: Quform MailChimp Integration
 * Description: Add subscribers to MailChimp from Quform forms.
 * Version: 1.0
 */
 
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {    if (!class_exists('MCAPI')) {
        require_once dirname(__FILE__) . '/MCAPI.class.php';
    }
 
    $api = new MCAPI('yourApiKey');    $listId = 'yourListId';    $email = $form->getValue('quform_1_4');    $mergeVars = array();
 
    // Set the first and last name (optional)
    $name = $form->getValue('quform_1_3');    $mergeVars['FNAME'] = $name[2];
    $mergeVars['LNAME'] = $name[4];
 
    // Other settings
    $emailType = 'html';
    $doubleOptin = false;
    $updateExisting = false;
    $replaceInterests = false;
    $sendWelcome = false;
 
    $api->listSubscribe($listId, $email, $mergeVars, $emailType, $doubleOptin, $updateExisting, $replaceInterests, $sendWelcome);
 
    return $result;
}, 10, 2);
<?php

/*
 * Plugin Name: Quform MailChimp Integration
 * Description: Add subscribers to MailChimp from Quform forms.
 * Version: 1.0
 */

add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
    if (!class_exists('MCAPI')) {
        require_once dirname(__FILE__) . '/MCAPI.class.php';
    }

    $api = new MCAPI('yourApiKey');
    $listId = 'yourListId';
    $email = $form->getValue('quform_1_4');
    $mergeVars = array();

    // Set the first and last name (optional)
    $name = $form->getValue('quform_1_3');
    $mergeVars['FNAME'] = $name[2];
    $mergeVars['LNAME'] = $name[4];

    // Other settings
    $emailType = 'html';
    $doubleOptin = false;
    $updateExisting = false;
    $replaceInterests = false;
    $sendWelcome = false;

    $api->listSubscribe($listId, $email, $mergeVars, $emailType, $doubleOptin, $updateExisting, $replaceInterests, $sendWelcome);

    return $result;
}, 10, 2);
  • On line 9, replace the number 1 with the form ID
  • On line 14, replace yourApiKey with your MailChimp API key, see Step 2 below
  • On line 15, replace yourListId the MailChimp list ID, see Step 2 below
  • On line 16, replace 1_4 with the Email element unique ID
  • On line 20, replace 1_3 with the Name element unique ID if the form has a Name element, otherwise lines 19-22 can be removed
1
2
3
4
5
6
7
8
9
10
11
12
13
14151617
18
19
2021
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
 
/*
 * Plugin Name: Quform MailChimp Integration
 * Description: Add subscribers to MailChimp from Quform forms.
 * Version: 1.0
 */
 
function my_mailchimp_integration(array $result, Quform_Form $form) {
    if (!class_exists('MCAPI')) {
        require_once dirname(__FILE__) . '/MCAPI.class.php';
    }
 
    $api = new MCAPI('yourApiKey');    $listId = 'yourListId';    $email = $form->getValue('quform_1_4');    $mergeVars = array();
 
    // Set the first and last name (optional)
    $name = $form->getValue('quform_1_3');    $mergeVars['FNAME'] = $name[2];
    $mergeVars['LNAME'] = $name[4];
 
    // Other settings
    $emailType = 'html';
    $doubleOptin = false;
    $updateExisting = false;
    $replaceInterests = false;
    $sendWelcome = false;
 
    $api->listSubscribe($listId, $email, $mergeVars, $emailType, $doubleOptin, $updateExisting, $replaceInterests, $sendWelcome);
 
    return $result;
}
add_filter('quform_post_process_1', 'my_mailchimp_integration', 10, 2);
<?php

/*
 * Plugin Name: Quform MailChimp Integration
 * Description: Add subscribers to MailChimp from Quform forms.
 * Version: 1.0
 */

function my_mailchimp_integration(array $result, Quform_Form $form) {
    if (!class_exists('MCAPI')) {
        require_once dirname(__FILE__) . '/MCAPI.class.php';
    }

    $api = new MCAPI('yourApiKey');
    $listId = 'yourListId';
    $email = $form->getValue('quform_1_4');
    $mergeVars = array();

    // Set the first and last name (optional)
    $name = $form->getValue('quform_1_3');
    $mergeVars['FNAME'] = $name[2];
    $mergeVars['LNAME'] = $name[4];

    // Other settings
    $emailType = 'html';
    $doubleOptin = false;
    $updateExisting = false;
    $replaceInterests = false;
    $sendWelcome = false;

    $api->listSubscribe($listId, $email, $mergeVars, $emailType, $doubleOptin, $updateExisting, $replaceInterests, $sendWelcome);

    return $result;
}
add_filter('quform_post_process_1', 'my_mailchimp_integration', 10, 2);
  • On line 14, replace yourApiKey with your MailChimp API key, see Step 2 below
  • On line 15, replace yourListId the MailChimp list ID, see Step 2 below
  • On line 16, replace 1_4 with the Email element unique ID
  • On line 20, replace 1_3 with the Name element unique ID if the form has a Name element, otherwise lines 19-22 can be removed
  • On line 35, replace the number 1 with the form ID

Step 2 – get your MailChimp API key and list ID

API key

Click here to get your MailChimp API key.

You can alternatively get in MailChimp at Account → Extras → API keys.

Get your MailChimp API key

List ID

In MailChimp go to Lists, then for the list you want to add the subscriber to click on the down arrow on the right hand side then click Settings.

Go to the MailChimp Lists page

Go to the settings for the MailChimp list

At the bottom of the settings the list ID is displayed.

Get your MailChimp list ID

Step 3 – install the MailChimp PHP client

Download the MailChimp PHP API from here and extract the zip file. Put the MCAPI.class.php file into the folder you created in Step 1 quform-mailchimp. Upload the quform-mailchimp folder to the web server at wp-content/plugins then go to the Plugins page within WordPress and activate the plugin Quform MailChimp Integration and you’re done.

Troubleshooting

If the integration does not work, double check the form ID and email element unique ID are correct in the code. Also make sure to test it with an email address that does not already exist in the list.

To debug integration you can log the response from MailChimp when the subscriber is added to the WordPress debug log. To enable the response logging add the code below to the function, above the line return $result;.

1
2
3
if ($api->errorCode && defined('WP_DEBUG') && WP_DEBUG) {
    Quform::log($api->errorCode, $api->errorMessage);
}
if ($api->errorCode && defined('WP_DEBUG') && WP_DEBUG) {
    Quform::log($api->errorCode, $api->errorMessage);
}

Then temporarily enable debug logging then submit the form again and check for an error message in the wp-content/debug.log file. Once you’ve solved the issue you can turn off debug logging.

Still having trouble? Head over to the forums.

Forums