A blog post

Tech tip: Campaign Monitor subscription form for WordPress/Woothemes

Posted on the 02 October, 2009 at 8:59 pm Written by in Blog, email marketing

I’m a huge fan of CMS theme agency Woothemes and of email newsletter service-provider Campaign Monitor. I use both to help serve my clients, and my clients’ own clients. The Chilli Chocolate Marketing site is itself a tweaked version of a Woothemes design, and the “Subscribe to our newsletter” feature on each page of the website has a Campaign Monitor subscription database beneath the surface.

Many recent Woothemes WordPress themes feature out-of-the-box support for Campaign Monitor in the form of a pretty “enter your e-mail address” widget.

Woothemes's Campaign Monitor widget in action

However, at present that widget has only one formfield and adds only email addresses to your database — the widget doesn’t allow for the collection of names as well.

Having names is really important when it comes to customer communications. I want to be able to begin emails to my subscriber John with “Dear John” and in some instances it’s nice to have the recipient’s name in the subject field too. In my experience campaigns that feature personalisation of subject line or salutation outperform those that don’t, often by a huge margin. So, while Woothemes’s built-in subscription applet is cool, it doesn’t meet my requirements. When a site visitor subscribes to one do my newsletters, I want to know his or her name!

And you know what? I also want to have that site-interaction recorded in the site’s Google Analytics stats.

So, what I want is a WordPress widget that

  • subscribes a visitor to a Campaign Monitor subscription list with name AND email address
  • logs a hit in Google Analytics
  • looks as good as the Woothemes-supplied email-only widget

The easiest way to insert content of any sort into the sidebar of a site running WordPress is to use a Text widget. So that’s the first task: find some code to use in a Text widget that enables subscribers to enter both name and email address, and pipe that info to Campaign Monitor.

Fortunately Campaign Monitor has done most of the hard work already with this AJAX subscription form utilising PHP, cURL, and jQuery. At the bottom of the page you’ll see a “bonus version” of the form tweaked for Google Analytics integration; that’s the one we want. Follow the instructions on that page.

If you follow the instructions correctly, you’ll end up with a static html page that subscribes a site visitor to your Campaign Monitor subscriber list and registers a pageview in your Google Analytics stats. So far so good. However since the javascript is within the <head> tag, the entire page’s code can’t be inserted into a Text widget.

So what we really need now is some way of inserting custom code – the form javascript – as a custom header in PHP pages generated by WordPress so that the Text widget only contains the form itself.

We need the plugin that no WordPress site administrator should ever be without: Semper Fi Web Design‘s All-In-One SEO Pack plugin. Aside from the fact that it’s an utterly indispensable SEO tool, AIOSEO also allows you to add, you guessed it, custom headers to pages within your WordPress site.

So, in the AIOSEO admin console, copy the segments of javascript from the working static html and paste them into the appropriate section — or all of them if, like me, you want to be able to put a subscription form on pages, posts, and the front page.

Paste the javascript into the All-In-One SEO pack admin console

Then create a Text widget, give it a title like “Subscribe to our newsletter”, and as its body paste in the subscription form itself.

Text sidebar widget containing form code

Position that Text widget in a widgetised space within your page. The form will show up in that space and, assuming you’ve pasted in your Campaign Monitor credentials correctly, it will function correctly.

However, it’ll look ugly:

Subscription sidebar widget, unstyled

What we want is for it to look as slick as Woothemes’s native widget.

This can be achieved with only a few characters of CSS (and a tweaked “Submit” input button). Change:


<!-- start -->
<div id="theForm">
<form action="http://YOURSITEADDRESS.createsend.com/t/r/s/xxxxx/" method="post" id="subForm">
<div>
<label for="name">Name:</label><br /><input type="text" name="cm-name" id="name" /><br />
<label for="xxxxx-xxxxx">Email:</label><br /><input type="text" name="cm-xxxxx-xxxxx" id="xxxxx-xxxxx" /><br />
<input type="submit" value="Subscribe" />
</div>
</form>
</div>
<div id="confirmation" style="display: none;">Thanks for subscribing</div>
<!-- end -->

…to:

<!-- start -->
<form action="http://YOURSITEADDRESS.createsend.com/t/r/s/xxxxx/" method="post" id="subForm">
<div>
<input type="text" name="cm-name" id="name" value="Name" size="15" style="border: 1px solid #d9d9d9;" />
<input type="text" name="cm-xxxxx-xxxxx" id="xxxxx-xxxxx" value="Email address" size="15" style="border: 1px solid #d9d9d9;" />
<input type="submit" value="Go!" style="border: 1px solid #d9d9d9;" />
</div>
</form>
</div>
<div id="confirmation" style="display: none; font-size:11px;padding-top:24px;">Thanks for subscribing!</div>
<!-- end -->

And the form will look like this:

Subscription sidebar widget, styled

…which is of course the way it looks on our site right now. And, of course, this will work in any WordPress site – not just Woothemed ones.

Mission accomplished. Thanks to the people behind WordPress, Woothemes, Campaign Monitor, and All-In-One SEO for their work!

  • jenny

    Bloody marvelous! I’ve been looking for this all morning…I just didnt know it! Well done and thanks.

  • mmtrav

    Whoa. You just wrote the whole manual for me. Thanks!

  • mmtrav

    Hmm. I’ve hit a bug. I’m using the Wootheme backstage and it doesn’t seem to like the script coding in the header on the home page. My slider stops working when this happens. Off to Woothemes forum for some assistance..

  • Simon

    Sounds like a clash of libraries – I bet the theme is loading jquery for the slider, and then the subscribe-form header lines are loading jquery again on top of that. I don’t know Backstage at all but a quick scan of what javascript libraries are being loaded is the first place I’d start.

  • mmtrav

    Hi Simon, you were right. I didn’t need to keep the jquery script code in there. The WooThemes guys sorted me out right away.

    Are you pretty proficient with customising Woothemes? If so I might like to get a quotation from you. I’ll use your contact form now. Cheers!

  • jon hemsley

    hey there did you get your slider working as i am having the same trouble and need a fix. Many thanks jon

  • Patrick

    Absolutely flawless and easy to follow. Thanks so much, this was the best solution out there. Keep up the good work. Cheers.

  • http://www.publikdesign.com Tim

    Hi mate, thanks for the great guide. I couldnt get it working as above, and found a 404 error in firebug when the POST starts, it looks for the proxy.php file included in the package from campaign monitor page you linked to above (couldn’t find ‘bonus version’ link).

    I put the proxy.php file in my sites root directory and it works great now.

    However was just wondering if this is safe to have the proxy.php file in the sites root directory?

  • Simon

    Hi Tim, sorry for the late reply. I can’t see any reason why it wouldn’t be safe. FYI I have proxy.php in the root chmod 644. Simon

  • http://www.publikdesign.com Tim

    Cheers Simon Thanks again for the tut.

  • http://relaxationcentre.co.uk Jon Lee

    I have a clash with LightBox Plugin and this Campaign Monitor signup code. Anyone else got this? It does the ugly confirm or error page rather than the smooth ajax bit. Does anyone know how to keep both?

  • Simon

    Hi Jon, like mmtrav’s problem detailed further up the page my first thought is that Lightbox is loading jquery and it’s clashing with another instance of jquery being loaded in the custom headers. Try commenting out one then the other to see if that resolves the issue.

  • http://androidapkfiles.blogspot.com Android Apk files

    Nice knowledge! I have been previously seeking something like this for a little bit now Many thanks!