Skip to content

Instantly share code, notes, and snippets.

@pingram3541
Last active August 27, 2019 17:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save pingram3541/9b0297b1e85deb7d1371f39876155bd2 to your computer and use it in GitHub Desktop.
Save pingram3541/9b0297b1e85deb7d1371f39876155bd2 to your computer and use it in GitHub Desktop.
MailerLite Integration w/ Resubscribe support - Elementor Pro Forms
add_action( 'elementor_pro/forms/new_record', function( $record ) {
// Make sure this is the correct form
$form_name = $record->get_form_settings( 'form_name' );
if ( 'YOUR_ACTUAL_FORM_NAME_HERE' !== $form_name ) {
return;
}
$submmited_fields = $record->get( 'fields' );
// Ref: https://developers.mailerlite.com/v2/reference#create-a-subscriber
$fields = [
'email' => $submmited_fields['ID_OF_YOUR_EMAIL_FIELD']['value'],
'name' => $submmited_fields['ID_OF_NAME_FIELD']['value'],
'resubscribe' => true,
'fields' => [
'FIELD_KEY' => $submmited_fields['ID_OF_A_FIELD']['value'],
],
];
// Add subscriber
// Use //https://api.mailerlite.com/api/v2/groups/{Group_ID}/subscribers to add directly to a group
$url = 'https://api.mailerlite.com/api/v2/subscribers';
wp_remote_post( $url, [
'headers' => [
'content-type' => 'application/json',
'x-mailerlite-apikey' => 'YOUR_ACTUAL_API_KEY_HERE',
],
'body' => $fields,
]);
});
//or if you have MailerLite PHP SDK :
add_action( 'elementor_pro/forms/new_record', function( $record ) {
if ( ! class_exists( '\MailerLiteApi\MailerLite' ) ) {
// Mailerlite SDK Missing
return;
}
// Make sure this is the correct form
$form_name = $record->get_form_settings( 'form_name' );
if ( 'YOUR_ACTUAL_FORM_NAME_HERE' !== $form_name ) {
return;
}
$submmited_fields = $record->get( 'fields' );
// Ref: https://developers.mailerlite.com/v2/reference#create-a-subscriber
$fields = [
'email' => $submmited_fields['ID_OF_YOUR_EMAIL_FIELD']['value'],
'name' => $submmited_fields['ID_OF_NAME_FIELD']['value'],
'resubscribe' => true,
'fields' => [
'FIELD_KEY' => $submmited_fields['ID_OF_A_FIELD']['value'],
],
];
// Add subscriber
$subscribers_api = ( new \MailerLiteApi\MailerLite( 'YOUR_ACTUAL_API_KEY_HERE' ) )->subscribers();
$added_subscriber = $subscribers_api->create( $fields ); // returns added subscriber
/*
* You can add directly to a group
$groups_api = (new \MailerLiteApi\MailerLite('API_KEY'))->groups();
$group_id = 123;
$added_subscriber = $groups_api->addSubscriber($groupId, $fields ); // returns added subscriber
*
*/
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment