A starting point for customising the WordPress Campaign Monitor Addon plugin, for using WordPress forms to maintain detailed contact lists in Campaign Monitor.
By extending the Contact Form 7 plugin with the Campaign Monitor Addon, and with a little help from my colleague Mouhannad Al-Sayegh, I was able to unlock a pretty powerful combination that enables all kinds of list management functions using the Campaign Monitor API.
Campaign Monitor
To start you’ll need an account with Campaign Monitor, and a demo list for testing purposes.
Contact Form 7
This plugin is super-flexible and easy to use if you know a little HTML and the rudiments of building forms. If you haven’t already you’ll need to install the plugin and create a demo form.
Campaign Monitor Addon
When installed and activated, this plugin appends a module to your form. No need for me to go into detail here – read Joshua Bettigole’s instructions and download his excellent manual on the steps required.
Connecting to Campaign Monitor
By default the Campaign Monitor Addon provides two fields for subscriber data:
- Subscriber Email
- Subscriber Name
It takes these fields and feeds them to the subscriberAdd method:
$result = $cm->subscriberAdd($email, $name);
Custom fields
So far so good – but I wanted to process additional fields from the form. Campaign Monitor allows you to add custom fields to lists (e.g. business name, country, gender, website URL, etc.) so I wanted to harvest this data directly from my contact form.
First of all I needed to modify cf7-campaignmonitor.php. I added new form fields to the existing list:
<div class="mail-field">
<label for="wpcf7-campaignmonitor-email"></label>
<input id="wpcf7-campaignmonitor-email" class="wide" name="wpcf7-campaignmonitor[email]" size="70" type="text" value="<?php echo esc_attr( $cf7_cm['email'] ); ?>" /></div>
<div class="mail-field">
<label for="wpcf7-campaignmonitor-name"></label>
<input id="wpcf7-campaignmonitor-name" class="wide" name="wpcf7-campaignmonitor[name]" size="70" type="text" value="<?php echo esc_attr( $cf7_cm['name'] ); ?>" /></div>
<div class="mail-field">
<label for="wpcf7-campaignmonitor-accept"></label>
<input id="wpcf7-campaignmonitor-accept" class="wide" name="wpcf7-campaignmonitor[accept]" size="70" type="text" value="<?php echo esc_attr( $cf7_cm['accept'] ); ?>" /></div>
<!-- Additional form fields -->
<div class="mail-field">
<label for="wpcf7-campaignmonitor-business"></label>
<input id="wpcf7-campaignmonitor-business" class="wide" name="wpcf7-campaignmonitor[business]" size="70" type="text" value="<?php echo esc_attr( $cf7_cm['business'] ); ?>" /></div>
<div class="mail-field">
<label for="wpcf7-campaignmonitor-website"></label>
<input id="wpcf7-campaignmonitor-website" class="wide" name="wpcf7-campaignmonitor[website]" size="70" type="text" value="<?php echo esc_attr( $cf7_cm['website'] ); ?>" /></div>
These can obviously be re-ordered to suit. Next you need to modify the main wpcf7_cm_subscribe() function below:
$email = preg_replace_callback( $regex, $callback, $cf7_cm['email'] );
$name = preg_replace_callback( $regex, $callback, $cf7_cm['name'] );
// initialise new field variables
$business = preg_replace_callback( $regex, $callback, $cf7_cm['business'] );
$website = preg_replace_callback( $regex, $callback, $cf7_cm['website'] );
// create array for additional fields
$extraFields = array('Business Name' => $business, 'Business Website' => $website);
Note that ‘Business Name’ and ‘Business Website’ should match the custom field names you have created in Campaign Monitor.
Finally, we need to modify the method call. In campaignmonitor.php are listed a number of PHP wrapper functions for Campaign Monitor API methods. It’s possible to use different method calls in place of subscriberAdd – for example we needed to use subscriberAddWithCustomFields. Our previously defined $extraFields variable follows the standard $email and $name:
$result = $cm->subscriberAddWithCustomFields($email, $name, $extraFields);
Campaign Monitor will check the email address against a given list. If it already exists, any new data will replace what is already in the record. Note that you need to re-supply any existing data, or it will be wiped. You may need to run a few tests to make sure your form submissions are putting the additional data in the right places.
Checkboxes etc.
The above example demonstrates form submission using custom fields with single values. But what happens when you want to process a set of checkboxes into a ‘Multi-option’ custom field in Campaign Monitor?
$checkboxes = preg_replace_callback( $regex, $callback, $cf7_cm['checkboxes'] );
The above line extracts the cleaned-up data from the form submission, but stores it as a string e.g. ‘category 1, category 2, category 3′ etc. What I wanted was to put each comma-separated value into its own option inside of a multi-option custom field – so an additional step was required:
$checkboxes = preg_replace_callback( $regex, $callback, $cf7_cm['checkboxes'] );
// explode the string into an array
$checkboxArray = explode(", ", $checkboxes);
// create array for additional fields
$extraFields = array('Business Name' => $business, 'Business Website' => $website, 'Categories' => $checkboxArray);
This is really just scratching the surface of what could be developed using these tools – but hopefully it helps people trying to move beyond the standard tutorials and installation instructions already out there.