Skip to content

Instantly share code, notes, and snippets.

@maor
Last active July 1, 2020 13:01
Show Gist options
  • Save maor/1c942b8d57c2dd016e3c6a936327dd71 to your computer and use it in GitHub Desktop.
Save maor/1c942b8d57c2dd016e3c6a936327dd71 to your computer and use it in GitHub Desktop.
Quick and dirty method I wrote up to integrate a Ninja Forms into Mailster, make it add subscribers to the main Mailster subscribers list. (WordPress)
<?php
// http://developer.ninjaforms.com/codex/custom-form-action/
// https://kb.mailster.co/mailster-for-developers/
add_action('init', 'ninjaforms_mailster_integration_init');
function ninjaforms_mailster_integration_init() {
if (!function_exists( 'mailster' )) return;
add_action( 'ninja_forms_mailster_integration', 'action_ninja_forms_mailster_integration' );
}
function action_ninja_forms_mailster_integration( $form_data ) {
if (empty($form_data['fields_by_key'])) {
return;
}
$fields_to_collect = array(
'firstname' => 'nome_1503181849365',
'lastname' => 'sobrenome_1503181864093',
'email' => 'e-mail_1503181872211',
'phone' => 'telefone_1503181882828',
);
// define to overwrite existing users
$overwrite = true;
// add with double opt in
$double_opt_in = true;
$fields_needed = [];
$userdata = [];
foreach ($fields_to_collect as $type => $fieldname) {
if (empty($form_data['fields_by_key'][$fieldname]['value'])) {
continue;
}
$userdata[$type] = trim($form_data['fields_by_key'][$fieldname]['value']);
}
// Add to mailster now
$userdata['status'] = $double_opt_in ? 0 : 1; //1 = subscribed (default) , 0 = pending, 2 = unsubscribed, 3 = hardbounced
// add a new subscriber and $overwrite it if exists
$subscriber_id = mailster( 'subscribers' )->add( $userdata, $overwrite );
}
@maor
Copy link
Author

maor commented Jun 18, 2018

Make sure you map fields correctly to the ones you have in your form.
See array on line 18.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment