MailerLite

This guide will show you how to add a new subscriber to MailerLite when a user submits a Quform form. In this guide we’ll create a new plugin to contain the code needed for the integration to work.

Create a new folder named quform-mailerlite and inside it create a file named quform-mailerlite.php

Inside the quform-mailerlite.php file add the following code.

1
2
3
4
5
6
7
8
9101112
13
141516
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
 
/*
 * Plugin Name: Quform MailerLite
 * Description: Add subscribers to MailerLite from Quform forms.
 * Version: 1.0.0
 */
 
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);
<?php

/*
 * Plugin Name: Quform MailerLite
 * Description: Add subscribers to MailerLite from Quform forms.
 * Version: 1.0.0
 */

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 9, replace the number 1 with the form ID
  • On line 10, replace fc7b8c5b32067bcd47cafb5f475d2fe9 with the MailerLite API Key, which you can find on this page
  • On line 11, replace 3640549 with the MailerLite Group ID, which you can find on this page under Subscriber groups
  • On line 14, replace 1_4 with the Email element unique ID
  • On line 15, replace 1_3 with the Name element unique ID
1
2
3
4
5
6
7
8
9
10
111213
14
151617
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
 
/*
 * Plugin Name: Quform MailerLite
 * Description: Add subscribers to MailerLite from Quform forms.
 * Version: 1.0.0
 */
 
function my_mailerlite_integration(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;
}
add_filter('quform_post_process_1', 'my_mailerlite_integration, 10, 2);
<?php

/*
 * Plugin Name: Quform MailerLite
 * Description: Add subscribers to MailerLite from Quform forms.
 * Version: 1.0.0
 */

function my_mailerlite_integration(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;
}
add_filter('quform_post_process_1', 'my_mailerlite_integration, 10, 2);
  • On line 11, replace fc7b8c5b32067bcd47cafb5f475d2fe9 with the MailerLite API Key, which you can find on this page
  • On line 12, replace 3640549 with the MailerLite Group ID, which you can find on this page under Subscriber groups
  • On line 15, replace 1_4 with the Email element unique ID
  • On line 16, replace 1_3 with the Name element unique ID
  • On line 32, replace the number 1 with the form ID

Once you’ve made the changes, you can zip up the quform-mailerlite folder and install it as a regular WordPress plugin by going to Plugins → Add New → Upload.

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 log the response from MailerLite to the WordPress debug log. To enable the response logging find this line:

1
wp_remote_post(
wp_remote_post(

And change it to:

1
$response = wp_remote_post(
$response = wp_remote_post(

Then, at the end of the function, find this line:

1
return $result;
return $result;

And above it, add this code:

1
2
3
if (defined('WP_DEBUG') && WP_DEBUG && wp_remote_retrieve_response_code($response) >= 400) {
    Quform::log(wp_remote_retrieve_body($response));
}
if (defined('WP_DEBUG') && WP_DEBUG && wp_remote_retrieve_response_code($response) >= 400) {
    Quform::log(wp_remote_retrieve_body($response));
}

Then temporarily enable debug logging then submit the form again and check for an error message in the wp-content/debug.log file. Once you’ve solved the issue you can turn off debug logging and revert the troubleshooting code changes from the code.

Still having trouble? Head over to the forums.

Forums