WordPress | Add checkbox for mailUp subscription in Contact Form 7

This time we had a customer that wants to migrate the newsletter platform form mailChimp to mailUp.

He have a WordPress website that use Contact Form 7 plugin, and inside the “contacts” form, had a checkbox that once accepted, subscribe the user to the newsletter mailing list.
We need to re connect this checkbox to the MailUp Platform.

Firsts download and open function.php file from your theme ,and open browser windows and navigate and connect to your mailUp account.

In function.php add this code :


<?php /* Conntect  Contact form 7 to MailUp */

 function mailup_subscription($formData) {
  $data = $formData->posted_data; // Get the data send by form
  if ($data['acceptance-203']) { // check for the "sucbscribe to newsletter flag ( ['acceptance-203'] ) 
      /* Set the parmaters */
      $mailup_console_url = 'a5d54a1.emailsp.com'; /* Your mailUp url  */
      $pars = 'list=1'; /* Id of the default list */
      $pars .= '&amp;email='.$data['your-email']; /* get the mail field */
      $pars .= '&amp;confirm=true'; 
      $pars .= '&amp;csvFldNames=campo1';
      $pars .= '&amp;csvFldValues='.$data['your-name']; /* get the name field */
      $mailup_subscription_url = "http://{$mailup_console_url}/frontend/xmlSubscribe.aspx?".$pars; /* sen data to MailUp */
      $ch = curl_init();
      curl_setopt($ch, CURLOPT_URL, $mailup_subscription_url);
      curl_setopt($ch, CURLOPT_HEADER, 0);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
      $result = curl_exec($ch);
      curl_close($ch);
  }
  return $formData;
}
add_filter('cfdb_form_data', 'mailup_subscription'); 

// example of your dashborad mailup url https://a5d54a1.emailsp.com/frontend/subscribe.aspx ?>

Basically this filter work in this way:

  • Get the data send by “Contact from” ( $data = $formData->posted_data; )
  • Check if newsletter checkbox is active ( if ($data[‘acceptance-203’]) { ), remember to change ” [‘acceptance-203’] ” with id of your checkbox in contact form 7
  • Get your dashboard mailUp url ( $mailup_console_url ), remember to change with the parameter that is from your dashboard url ( simply connect to your mailUp account and copy the dashboard url )
  • Get the List ID ( $pars = ‘list=1’; ), update with the id list from mailUp
  • Get the e-mail filed ( $pars .= ‘&email=’.$data[‘your-email’]; ), update with the email field ID from contact form 7
  • Get the name filed ( $pars .= ‘&csvFldValues=’.$data[‘your-name’]; ), update with the email field ID from contact form 7
  • Compose URL for transmit data to mailUp ( $mailup_subscription_url = “http://{$mailup_console_url}/frontend/xmlSubscribe.aspx?”.$pars; )
  • Send the data to MailUp ( curl_setopt($ch, CURLOPT_URL, $mailup_subscription_url); )

If all it’s right, from now users that flag “Subscribe to our newsletter” will be subscribe to your MailUp Account ( $mailup_console_url ) in the selected list ( $pars = ‘list=1’; ) with Name ( $pars .= ‘&csvFldValues=’.$data[‘your-name’]; ) and Email ( $pars .= ‘&email=’.$data[‘your-email’]; ) that are used for compile the contact form.

Make a test by compiling the contact form, and login in MailUp dashboard, you’ll see a news user correctly subscribed ?

That’s all