We recently had an inquiry from a client looking to integrate their MailPoet user subscription list with one of our favorite tools in our Toolbox, Formidable Pro. Turns out, after a little research, it’s actually quite easy. In this Lab post, we use a Formidable Pro form to add users to a an existing MailPoet mailing list. MailPoet makes it very easy to use third party forms (Formidable Pro, Gravity Forms, Contact 7 .. or forms you make your self). We are going to use the sample code provided from the good folks at MailPoet and a form we created using Formidable Pro to capture the user data and send it to the mailing list specified.

Lets take a look at the MailPoet supplied code.

[code language=”php”]

function processMyForm(){

//you could make some validation even though validation
//is also processed in our function
$my_email_variable = $_POST[‘my_email_variable’] ;
$my_list_id1 = $_POST[‘my_list_id1’] ;
$my_list_id2 = $_POST[‘my_list_id2′] ;

//in this array firstname and lastname are optional
$user_data = array(
’email’ => $my_email_variable,
‘firstname’ => $firstname,
‘lastname’ => $lastname);

$data_subscriber = array(
‘user’ => $user_data,
‘user_list’ => array(‘list_ids’ => array($my_list_id1,$my_list_id2))
);

$helper_user = WYSIJA::get(‘user’,’helper’);
$helper_user->addSubscriber($data_subscriber);
//this function will add the subscriber to mailpoet

//if double optin is on it will send a confirmation email
//to the subscriber

//if double optin is off and you have an active automatic
//newsletter then it will send the automatic newsletter to the subscriber
}

//initialize this function only when your subscription form data has been posted
add_action(‘init’,’processMyForm’) ;

[/code]

The WordPress ‘init’ Action Hook

First thing to take note of is that this function processMyForm uses the WordPress ‘init’ action hook. This means that, once this is added to your functions.php file, this function will execute on each and every page request (or in more common parlance, every time a page is loaded on your site).

What does this mean to us then?… it means by simply submitting the form and supplying appropriate variables in the returned page after submission, MailPoet will automatically fire the processMyForm function find the variables and execute accordingly.

It also means that ANYTIME a page loads with the “$_GET” request variables “?my_email_variable=something&my_list_id1=something&my_list_id2=something.. it will trigger the processMyForm function. You will likely want to control this to one degree or another.

It should be noted in the sample code that these form variables are expected as $_POST values. We can adjust the code to accept $_GET values appended to the URL string —  for example  http://youraddress/somepost/withform/?my_email_variable=someemail@test.com&my_list_id=1&firstname=rob&lastname=keenan or to be even more flexible we could adjust the code to look for $_REQUEST

[]  variables – POET or GET variables. Here is the revised code…

[code language=”php”] function processMyForm(){
$email = $_REQUEST[‘sub_email’] ;
$list_id1 = $_REQUEST[‘list1_id’] ;
$list_id2 = $_REQUEST[‘list2_id’] ;
$user_data = array(
’email’ => $my_email_variable,
‘firstname’ => $firstname,
‘lastname’ => $lastname);

$data_subscriber = array(
‘user’ => $user_data,
‘user_list’ => array(‘list_ids’ => array( $list_id1,$list_id2 ))
);

$helper_user = WYSIJA::get(‘user’,’helper’);
$helper_user->addSubscriber($data_subscriber);
}
add_action(‘init’,’processMyForm’) ;[/code]

The Formidable Pro form

The Formidable Pro form can contain any number of variables we want. In the sample code from MailPoet, the function is expecting to store three data points for each MailPoet subscriber: First Name, Last Name, Email Address to several lists (my_list_id1 & my_list_id2). Lets expand this to also include City, State/Province and Country so we can ensure enough data in our subscriber list to effectively segment our lists and send tailored newsletters to those in different cities, states and countries.

Once the publicly visible user input fields are created with Formidable Pro, we will want to add one last hidden field to the form to specify the MailPoet LIST ID we want the user data stored to. In this sample case I created a subscriber list for the demo with an ID of 4.

Here is a rough and ready screen cap of the build screen inside Formidable Pro
BSN Labs: Submit to MailPoet using Formidable Pro

(N.B. We could offer a more than one list and create checkboxes with the value set to the various list ID’s available for users to join. In that case we wouldn’t need the hidden list ID field, and use the checkbox values instead.)

Ok.. so below is our finished form (one might go so far as to call it a font-Awesome form). To get this to basically work we simply need to submit the form, letting Formidable Pro do what it does (save user submitted content) and return those submitted data points on a results page as either a GET request variable or a POST request variable.

In this demo, we have decided, for expediency sake, to take the path of least resistance and return these variables as GET parameters appended to the post-submission results page. Again, this is very simple, in the “settings” -> “General” screen in Formidable Pro admin area for the form, we adjusted the “Action after Form Submission” dialog to append the form values onto the URL of the current page (in our case post id 1704.. thus the /?p-1704 syntax)

Here is another rough and ready screen cap of that page.
labs_MailPoet_form_settings

..and that is it. Here is the complete form. Just below the form I have a MailPoet shortcode indicating the current number of users on this list

[wysija_subscribers_count list_id="4" ]

. You should see the number increment with each submission.

Curent Number of Subscribers: [wysija_subscribers_count list_id=”3″ ]