Add Subscribers Through your own Form or Plugin

You need to be familiar with PHP code and POST data to make this work. If you require assistance, you can ask for it on codeable.io or wpquestions.com for a small fee.

You can integrate MailPoet into your own code or plugin to:

  1. create your own subscription form
  2. add a checkbox to an existing form

Steps to follow to make it work :

  1. Have a form on your frontend
  2. Create a function to process the posted data when submitting the form
  3. The function from step 2 needs to be called once MailPoet core has been loaded (the init hook is your friend)
  4. load that function only when needed. That means only when data has been posted using your subscription form.
  5. Your form processing function should look like this to add subscribers :
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') ;

To display the lists

You might want to let your users select which lists to display, like this example in Magic Action Box :

Here is an example code :

//this will return an array of results with the name and list_id of each mailing list
$model_list = WYSIJA::get('list','model');
$mailpoet_lists = $model_list->get(array('name','list_id'),array('is_enabled'=>1));

//this loop will just echo the information selected for each list

foreach($mailpoet_lists as $list){
    echo 'list id : '.$list['list_id'].' , list name : '.$list['name'] ."<br/>";
}

Note: for this code to work mailpoet needs to be loaded so you need to use a hook accordingly. init will work.

0