This guide will show you how to add a new subscriber to MailerLite when a user submits a Quform form.

Add the following code to the WordPress theme functions.php file (or use a plugin).

1234
5
678
9
10
11
12
13
14
15
16
17
18
19
20
21
22
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {    $apiKey = 'fc7b8c5b32067bcd47cafb5f475d2fe9';    $groupId = '3640549'; 
    $data = array(
        'email' => $form->getValueText('quform_1_4'),        'name' => $form->getValueText('quform_1_3')    );
 
    wp_remote_post(
        sprintf('https://api.mailerlite.com/api/v2/groups/%s/subscribers', $groupId),
        array(
            'headers' => array(
                'content-type' => 'application/json',
                'x-mailerlite-apikey' => $apiKey
            ),
            'body' => wp_json_encode($data)
        )
    );
 
    return $result;
}, 10, 2);
add_filter('quform_post_process_1', function (array $result, Quform_Form $form) {
    $apiKey = 'fc7b8c5b32067bcd47cafb5f475d2fe9';
    $groupId = '3640549';

    $data = array(
        'email' => $form->getValueText('quform_1_4'),
        'name' => $form->getValueText('quform_1_3')
    );

    wp_remote_post(
        sprintf('https://api.mailerlite.com/api/v2/groups/%s/subscribers', $groupId),
        array(
            'headers' => array(
                'content-type' => 'application/json',
                'x-mailerlite-apikey' => $apiKey
            ),
            'body' => wp_json_encode($data)
        )
    );

    return $result;
}, 10, 2);
  • On line 1, replace the number 1 with the form ID
  • On line 2, replace fc7b8c5b32067bcd47cafb5f475d2fe9 with the MailerLite API Key, which you can find on this page
  • On line 3, replace 3640549 with the MailerLite Group ID, which you can find on this page under Subscriber groups
  • On line 6, replace 1_4 with the Email element unique ID
  • On line 7, replace 1_3 with the Name element unique ID

Only subscribe if the user checks a Checkbox

In the code above, find this line.

1
$apiKey = 'fc7b8c5b32067bcd47cafb5f475d2fe9';
$apiKey = 'fc7b8c5b32067bcd47cafb5f475d2fe9';

Above it, add the following code.

1
2
3
if ($form->getElement('quform_1_5')->isEmpty()) {
    return $result;
}
if ($form->getElement('quform_1_5')->isEmpty()) {
    return $result;
}
  • Replace 1_5 with the Checkbox element unique ID

Only subscribe if the user checks a Radio Button

In the code above, find this line:

1
$apiKey = 'fc7b8c5b32067bcd47cafb5f475d2fe9';
$apiKey = 'fc7b8c5b32067bcd47cafb5f475d2fe9';

Above it, add the following code:

1
2
3
if ($form->getValue('quform_1_5') != 'Yes') {
    return $result;
}
if ($form->getValue('quform_1_5') != 'Yes') {
    return $result;
}
  • Replace 1_5 with the Radio Button element unique ID
  • Replace Yes with value of the radio button option that should subscribe the user

Troubleshooting

If the integration does not work, double check the form ID and Email element unique ID are correct in the code. Also make sure to test it with an email address that does not already exist in the list.

To debug integration you can temporarily install the plugin Log HTTP Requests. Once that plugin is installed and active, test the form submission again, then go to Tools → Log HTTP Requests. Find the request to api.mailerlite.com and click into to, look at the Response section to see if there is an error message.

Be inspired. © 2022 ThemeCatcher Ltd. 20-22 Wenlock Road, London, England, N1 7GU | Company No. 08120384 | Built with React | Privacy Policy