How to Build an Autoresponder – the Opt-in Form

How to create an email opt-in form in WordPressI wrote last week about how I decided a while back to see if I could build my own opt-in email autoresponder system as a WordPress plugin. The first part that I chose to figure out was the double opt-in component, which I will describe here.

Before I get into that, I want to point out some potential limitations of building an email autoresponder into a WordPress plugin. I don’t think the actual opt-in process will be that significant because it is only handling traffic that is already on the website. It just involves sending an extra email message and and an extra page load as the visitor completes the double opt-in.

All of the opt-in data and scheduled emails will be stored in the WordPress database. This could become a performance issue as you add thousands (hopefully) of new contacts to the database.

Having WordPress handle sending broadcast emails to those thousands of subscribers will be a bigger issue. First is the actual processing load from sending thousands of emails. The second issue is the actual logistics of sending thousands of emails. Most personal email accounts limit the number of emails that can be sent in a period of time to discourage spamming.

This solution should work for a small list of up to a few hundred subscribers as long as there is a mechanism in place to space out the emails so that they are sent over a few hours, or possibly through a bulk email service. I haven’t looked into that yet.

As I said last time, my goal with this project is to learn about how email autoresponders work so that I can make the best use of one when I go back to an established service like Aweber or MailChimp.

One More Thing Before We Get Started

It isn’t my intent here to teach you how to write PHP code (the programming language that all of the WordPress scripts are written in) or all of the inner workings of WordPress. I more want to point you in the right direction to learn more if you want to.

The Opt-in System

Ok, on to the opt-in system. First, let’s take a look at the sequence of events that have to happen to get a subscriber completely opted in so that they can receive emails from us.

  1. Display a form on the page to capture the subscribers info
  2. Receive the form submission
  3. Validate the data, prevent multiple opt-ins
  4. Display a thank you page or error page as appropriate
  5. Send a confirmation email with a link for the subscriber to confirm the opt-in
  6. Process the subscriber clicking on the link from the confirmation email
  7. Display another thank you page once the subscriber is completely in

Display the Opt-in Form

For the actual form, I cheated. I copied the HTML and CSS from an old Aweber form that I had in the past and embedded it as a short code in my new plugin. I am more than happy to let someone else handle all of the intricacies of writing the CSS.

If you have used WordPress for awhile then you probably already know what a short code is. Just in case you don’t, I’ll explain it very briefly. A short code allows you to embed some piece of functionality into your post. For example, boxes like the one below for tweeting a message to Twitter have become popular.

Have you read about this crazy guy who is writing his own email autoresponder? Click To Tweet

A short code is written between square brackets and starts with the name of the short code followed by any attributes that it supports. You can see the entire short code that created the tweet box here:

[bctt tweet="Have you read about this crazy guy who is writing his own email autoresponder?"]

This particular short code is named bctt and takes one attribute, tweet, which provides the message that you want your visitors to tweet. This short code is provided by the Better Click to Tweet plugin by Ben Meredith.

A short code seemed the easiest way to be able to display my opt-in form within my content, and writing a short code isn’t too hard.

The Pieces of a Short Code

The first part of creating a short code is the PHP function that will process the parameters and generate the content that will display in the finished post in place of the short code. The second piece is to inform WordPress about the short code so that it knows to call your function when it sees that short code in a post.

In this case, I designed my short code to look like this:

[[ywoptin campaign=’test’/]]

I named the short code ywoptin and it takes one attribute called campaign which specifies the name of the list campaign that the opt-in form is for, in this case it is my “test” campaign.

The PHP function declaration looks like this:

function ywoptin_shortcode( $atts, $content = null )

The name of the PHP function here is ywoptin_shortcode and WordPress requires short code functions to take two parameters: $atts is an array containing the attributes (campaign and its value, test) and $content is all of the content that is between the open and close tags for the short code. I don’t have any with my short code, so I set it to null.

If my short looked like this: [[ywoptin]do ray mi fa so la ti do[/ywoptin]] then $content would contain “do ray mi fa so la ti do.”

The function just contains the HTML to display the opt-in form and sets the campaign name as a hidden input field in the form. The final HTML is then returned to WordPress.

We now have to let WordPress know that when it sees a short code called ywoptin, it should call the ywoptin_shortcode function to handle it. We do this by calling the add_shortcode function, which is defined by WordPress. It looks like this:

add_shortcode(‘ywoptin’, ‘ywoptin_shortcode’);

It’s pretty self-explanatory. The first parameter is the name of the short code and the second parameter is the name of the function to be called.

Here’s what the opt-in form looks like so far. It will successfully add you to a list that I will use to notify you the next time I write a post in this series.

[ywoptin campaign=’autoresponder’/]

Next Time

That is enough to get the opt-in form to display. That was the easy part. We still have to do something with it once the subscriber clicks the button after they enter their name and email address. I struggled for awhile to figure out how to do that from a post or a page. I will go into that next time. I intend for that to be up next Thursday night or Friday morning.

If you missed the first part in this series, please read about why I’m taking on this project.

I would love to hear any thoughts you have on this little project of mine, even if it is just to call me crazy. Leave me a comment below, and I always appreciate it when you share my posts to your followers on social media.

This entry was posted in Blogging and tagged , , , , , , , . Bookmark the permalink.

12 Responses to How to Build an Autoresponder – the Opt-in Form

  1. Hey Ben,

    It is crazy, but very impressive, but I personally like using autoresponder plugins as I am not tech geek, and having very limited knowledge of coding, plugins help me achieve what could take years to learn, if I do by coding.

    But I must appreciate your work, you are really a rockstar.

    • Ben says:

      Thanks Jeannette. I use a real autoresponder for the things that matter too. There are capabilities that their servers have that I cannot replicate through my hosting account. Doing this kind of thing satisfies my need for creativity and gives me a HUGE appreciation for everything that the real autoresponders handle for you behind the scenes.

  2. Stephen says:

    Hi Ben,

    You state that opt-in data and scheduled emails will be stored in the WordPress database and that could have performance issue with a large mailing list.

    Is it not possible to have a separate database for your mailing list? I ask because I am trying to figure out how to incorporate SQL data into my WordPress posts. And this data will not be, I don’t want it to be, in my WordPress database.

    • Ben says:

      Hi Stephen. Yes, it is possible to access other databases. I chose to create the tables in the WordPress database because WordPress already handles that connection, so I don’t have to do any extra work to manage separate database connections. I don’t think the extra data itself being in the WordPress database as opposed to another database will be the biggest issue. Regardless of where the data is stored, you still have all of the traffic accessing the data to send emails to thousands of people on a large list. I think all of that traffic between the server running WordPress, the server running the database, and the email server will be the performance issue, especially on top of a blog that is active enough to have created a list of thousands of subscribers. I’m pretty sure that Aweber and the others have a set of servers that assemble and send the emails separate from their web servers that handle their customer traffic.

      • Stephen says:

        Hi Ben,

        You answered my question beautifully, eloquently. Now may I ask another?

        How easy is it to access a separate database to populate a dynamic post, displaying the query results both in the post and in the header/title?

        I know this is straying from the topic but, I’m sure there must be others out there pondering the same problem.

        • Ben says:

          Hi Stephen,

          I did some searching into that question yesterday when you mentioned it. There is an object in the WordPress code referred to by $wpdb. It makes it very easy to access your WordPress database, so the easiest solution is to put the data you want into that database, which is why I did it that way.

          I found this solution which allows you to create a new instance of the wpdb class so that you can access your other database with the same ease as you can the WordPress database. I haven’t tried it myself, but it looks good.

          • Stephen says:

            Hey Ben,

            You are a STAR!

            That looks like it will do almost exactly what I require.

            When I come to test it I will do so on a test WP installation so that any unavoidable mishaps are not catastrophic.

          • Ben says:

            Thanks Stephen. I don’t know that I’m a star. I just know how to find other people who have done something similar to what I want to do and use them as a starting point. I have been a programmer for more than 30 years and very rarely start anything from scratch. Good luck on your project.

  3. Elise Moreau says:

    Hi Ben,

    Some people might say it’s crazy but I don’t think it is. It’s a cool learning experience, and if you’re really interested in doing it, then why not? Seems like you laid out a lot of good reasons from your previous post.

    I love WordPress and web publishing but mostly stay away from the technical side of things as much as I can, even though I know there would be so many benefits to learning how to code properly. My sister is a web developer so I’m sure she could help me along with all that if I really wanted to dive right into it (and if I bribed her somehow).

    For now I stick to Aweber, but I’ve worked with MailChimp too and recently got a month of free access to ConvertKit from a webinar that Pat Flynn hosted. Apparently it’s the cool new email subscription platform all the cool kid marketers are using? Well, something like that! Sigh, just something else I need to add to my to-do list…

    Good luck with the next stage of your project!


    • Ben says:

      Thank you, Elise. I was a web developer too, so this is how I feed that part of me that would quickly get bored with just blogging. As I said in my post, there is an upper limit to the size of the list that this solution will be able to handle. So I will probably go back to Aweber too, but with a much greater appreciation for everything that they provide with their service. Thanks for taking the time to visit and for leaving a comment. I appreciate it.

  4. Chris Hooker says:

    Yeah, OK, you’re crazy! But crazy in a good way. Personally I have neither the time nor ‘bent’ to take on this particular type of project (maybe 40 years ago but then it wouldn’t have been available to do!). Fascinated to see how it goes Ben. You were good enough to visit my blog so I thought I would reciprocate.
    I’m afraid that what you are talking about is ‘all Greek’ to me but I I follow along, in time it won’t be.
    Keep up the good work.

    • Ben says:

      Hi Chris. I’m trying to keep the really technical stuff out of the posts, but it’s kind of difficult to do. I’ll be honest, the real reason I’m writing the posts is to document my process so that I have something to refer to down the road when I look at the work I did and can’t figure it out. And since I am writing, why not share it. I’m hoping to have my opt-in form cleaned up enough to put into my next post. There won’t be much point to opting in yet, but people will be able to see that it works.

      BTW, I just visited your website again a little bit ago and enjoyed the article you shared about email marketing. It fits in very well with what I’m doing here.

Leave a Reply

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

CommentLuv badge