How to Create a Viral Launch Page with WordPress

Viral launch pages are turning up everywhere and for good reason. They are an elegant and effective way to turn what could be a business's least-effect time online (no website or product ready yet) into the most effective marketing opportunity. Little explanation is required to create a lot of buzz, especially when there's an incentive thrown in there. Leverage the power of mystery! Also, people like free stuff.


In this tutorial I'm going to show you how to code up a quick viral launch page using WordPress. The tutorial is based off the code used for a theme we developed last month for WordPress called Launch Effect. You can see that theme in action here: Launch Effect Demo.

The Launch Effect theme allows the site owner to customize a landing page that introduces their business, product, or event and prompts visitors to sign up with their email to earn some sort of incentive (beta invites, gift cards, discounts, etc). Upon submission, the page generates a unique referral link that the site owner can use to track their most active referrers in order to know who to reward. Sign-up, visit and conversion data are all stored within the site owner's database and outputted to a private stats page.

Using a "light" version of the Launch Effect theme, this tutorial will show you how to accomplish the same email capture and unique URL generation functionality, as well as set up the stats page within the WordPress admin.

Skill-wise, this is more of a beginners PHP tutorial that gets you talking with a database and doing what you want with the data. We'll be using the proprietary functions you'll need to get things working inside of WordPress, specifically, though this basic knowledge is surely transferable to non-WordPress contexts. I hope the tutorial will be a helpful beginners' project for front-end coders looking to beef up their development knowledge beyond HTML/CSS.

Source Files

The tutorial source files are actually a standalone WordPress theme. To install:

  1. Upgrade a fresh install of WordPress to the latest version.
  2. Download and activate the theme.
  3. Go to Settings > Permalinks and select the third option.

The theme should look like the photos and function right out of the box. The stats data is available via the Launch Stats menu within the WordPress admin.

Though not necessary for this tutorial, it's always a good idea to have some kind of access to your database so you can get comfortable with it and have more power to troubleshoot future issues.

You'll Learn How To

  • Create a table for your data within the same database that holds your WordPress install
  • Validate and submit a form on a WordPress page with smooth Ajax action
  • Log visits and conversions using a unique referral link generated by your site
  • Output the form data to a page for viewing

Step 1 The Table

Referenced Files:

  • functions.php
  • functions/le-tables.php

Within the tutorial theme folder you'll notice that the functions.php file "requires" three additional files located within the functions folder. One of them, "le-tables.php," takes care of the actual creation of the new table within your database. If you've already activated the tutorial theme, your new table has already been created in the database. This is because the table creation is triggered simply when you VISIT any page within your WordPress site (visiting a WordPress page calls functions.php which calls le-tables.php).

Below, I go through and explain the different parts of the le-tables.php code that creates the database table. You can create any number of additional tables via this method. I strongly urge you to check out this entry in the WordPress Codex for the full scoop: WordPress Codex: Creating Tables with Plugins


Connect to the database with the WordPress global variable $wpdb. This instantiates a class already set up by WordPress to talk to the WordPress database. Always connect this way when you're working from inside WordPress.

Our table is going to be named "wp_launcheffect" and will be referred to by the $stats_table variable throughout the theme.

Check that the table doesn't already exist before creating it.

This $sql variable specifies the table structure. We're creating the following columns: id, time, email, code, referred_by, visits, conversions, and ip.

You're actually going to be using this WordPress dbDelta function to execute the SQL query. This function is not covered in this tutorial but basically it's in charge of making sure updates go smoothly if you have built a plugin, for example. Ideally it's supposed to compare current table structure with an updated one and add or modify as necessary.

Why not create a few variables to submit test data to the new table?

Upon creation of the table, insert the test data into the table. Running the variables through the $wpdb->escape function can help prevent security issues (but obviously the test data is no threat).

Once again we use WordPress's $wpdb class to perform the actual database manipulation. $wpdb->query is the broadest of all the built-in WordPress database manipulation functions. It allows you execute any SQL query on the WordPress database, on both WordPress-created tables or tables such as the one you just created. There are others for more specific purposes though, such as $wpdb->get_results, which we'll use to pull a results from the database as an array of rows.

Step 2 The Form

Referenced Files:

  • header.php
  • index.php
  • post.php
  • js/init.js
  • functions/le-functions.php

Our form has only one user-facing input—the email address signup. Upon submit, the form is hidden and a container is revealed with a success message and the unique referral link generated by the theme. The unique referral link is simply 5 random characters attached to the end of your site's base URL.

The functionality of this form is exactly what we use to power our Launch Effect theme. Let's dive into the files!


In this file, please just note the super-important line below which prevents the page from throwing a 404 status code (visible in Firebug) when someone accesses via a unique referral link.


This is the front page of your site—all that the visitor sees and interacts with is contained within this page. This file contains a straightforward form with a hidden success container and an error div to hold any error messages. The form tag should have an ID but the action should be left blank. jQuery takes care of the submission part and identifies the form via its ID.

Create a session so that we can store information inside of session variables across pages. In this case we'll be storing the "referred by" code. This means, if the visitor is arriving to your site via a referral link (a link with the unique code at the end), that information will be captured and passed along to the database.

If a visitor was referred by someone and arrived to the page via a unique referral link, the last 5 characters of the URL (the code) are stored within this session variable. (Please refer to the le-functions.php section for explanation.)

If a visitor was referred by someone, this function logs as a visit everytime someone comes the page via that unique referral link. (Please refer to the le-functions.php section for explanation.)

We'll store the base URL in this span so that our Ajax function can pick it up and use it to build a proper unique referral link. This is so we don't have to hardcode the URL in the javascript.

Pre-Signup Form This container disappears after the visitor submits their email address. The hidden inpput stores the unique, random 5-character code that will henceforth be associated with this visitor's email. If the email is invalid, that message will appear within the error div.

Post-Signup If they are a new signup, this container appears after the visitor submits their email address. The unique, random 5-character code will be paired with the base URL we stored in span.dirname and output within the successcode input as the unique referral link.

Returning Visitor If they are a returning visitor, this container appears after the visitor submits their email address. This contains empty spans where the javascript can stick specific data pulled from database about the returning visitor.


This file works in tandem with the javascript in js/init.js to post the form data. Credit goes to this super helpful tutorial for the functionality that we have modified for our needs: jQuery Ajax and jQuery Post Form Submit Examples with PHP.

Since this file is not a post or a page, WordPress doesn't really know it exists. Therefore, we have to bring it back into the loop just enough to talk to the WordPress database by including the following files:

Resuming the current session where we're storing our "referred by" code...

Variable for our "referred by" code

Set up the variables we'll use to store data from our form.

Check to see if an email has been submitted, and then if that email passes PHP's built-in email validator.

If it passes, set the $email_check variable to valid. We'll need this variable set to valid in order for our javascript to know it can submit the form.

Check to see whether visitor is a returning user (Please refer to the le-functions.php section for explanation.)

If the visitor is a returning visitor, mark $reuser variable as true and grab stats for that visitor from the database using getDetail function. (Please refer to the le-functions.php section for explanation.)

If the visitor is a new signup, mark $reuser as false and insert the form data into the database using the postData function. (Please refer to the le-functions.php section for explanation.)

Store the email_check data, each piece of form data, and our returning visitor data (if applicable) as an array in a JSON-formatted string

Echo the JSON string response for use by the javascript file. If you're using FireFox have Firebug installed, you can actually see the response in the console if you expand the POST item.


We're using this bit of jQuery to take of the actual form submission. Once again, take a look at this excellent tutorial which was the basis of this functionality: jQuery Ajax and jQuery Post Form Submit Examples with PHP.

When the user presses the submit button within the #form form...

Encode all the data in #form as a standard query string with jQuery's "serialize" method.

This is the function that's going to allow us to post data without a page refresh:

If the email address doesn't validate: Insert an error message into the empty #error div. Notice how data.email_check contains our valid/invalid information as passed from post.php. In the same way, data.code, data.returncode, data.clicks, data.conversions etc... will be accessible to us from here.

If returning visitor: Hide the pre-signup form and fade in the returning visitor container.

Format the unique referral link attaching the base URL we stored in that span in index.php to the 5-character code (data.returncode).

Stick the info about the returning visitor we pulled from the database into the following spans. That way, the returning visitor can see how many people clicked and signed up with their link so far.

Output the unique referral link onto the page.

If a NEW signup: Hide the pre-signup form and fade in the success container.

Format the unique referral link attaching the base URL we stored in that span in index.php to the 5-character code (data.code).

Output the unique referral link onto the page


This file defines all of the PHP functions used throughout the tutorial theme in a centralized location. I'll explain each one here.

Random Code Generator: We generate the random 5-character code with this function. Pretty straightforward...

Get "Referred by" Code: This grabs the current URL and puts it in the $url variable. We use PHP's parse_url function to grab the path portion of the URL, then we use the substr function to grab the last 5 characters of that URL portion. If the last 5 characters contain a slash or are empty, then it's a direct visit (no referral code). If not, then the "referred by" code is those last 5 characters.

Query: This just globalizes $wpdb so we can have access to all the WordPress database functions and creates an easy shorthand for specifying the SQL query ($query) and type of function ($type). For our forms our $type will be query, get_results, prepare, or get_var. See this Codex document to learn about all of the database functions available to you in the $wpdb class: WordPress Codex: WPDB Class Reference.

Log Visits via Referral Link: If a visitor was referred by someone, this function logs visits every time someone comes the page via that unique referral link. It increments the visits column by 1 wherever the referred_by column is equal to the $referredBy code grabbed from the URL.

Insert Data: The first half of this function takes care of actually inserting a new email and code pair into the database, along with the time. This also sets visits and conversions to 0. When someone visits the site via this person's unique referral link, those columns will be incremented. If the new signup was a result of someone's referral link, $update2 takes care of incrementing the conversions column by 1 for that person (same logic as logVisits only for conversions instead of visits).

Count: We use this function in a couple ways: in post.php to see whether the visitor is a returning visitor, we check whether or not the email address posted via the form matches any email addresses in the database table (e.g. $entry = email, $value = $_POST['email']). If so, then the count of such instances would be greater than 0, and the visitor would be a returning visitor. We also use it in index.php for the codeCheck function (see below) to check whether or not a code generated by the randomString function has already been used (unlikely but safe to check anyway).

Get Data: This simply grabs everything from our table. The get_results function type stores the results in an array.

Get Data by Value: This grabs everything from our table that matches a certain parameter. We use this in a couple of places: in post.php, if a visitor is a returning visitor, we return all data where the posted email address equals the email address already existing in the database. This returns visits and conversions data for that visitor. We also use it in in the Launch Stats admin page to see specific data for one visitor.

Step 3 The Stats

Referenced Files:

  • functions/le-stats.php

As you've no doubt discovered, the stats page is located as a top-level menu item in the WordPress admin. We'll pull the our data into a nice table on this page where you can drill down to individual people. Extra credit for pagination and sorting!

Need to redefine this variable for our table here again.

If the URL contains a "view" query string, we're actually going to be looking at a specific visitor's detail page (a page that shows everyone that signed up so far as a result of their link), as opposed to all of the stats at once. Visitors are catalogued by their 5-character codes, so the query string would look like "?view=XXXXX" at the end of the URL.

Set the $view variable equal to the query string's value.

Query the wp_launcheffect table for entries where the "referred_by" column is equal to $view and store it in the $results variable. Basically, show us the people that have signed up as a result of this visitor's unique referral link. (Please refer to the le-functions.php section for explanation of the function.)

If there aren't any results, say so.

If the URL does not contain any query strings, just show the main stats page. Get all the data from the wp_launcheffect table and store it in the $results variable. Display the contents of the $results array using a foreach loop.


I hope this was helpful and look forward to hearing your comments!



Related Articles