Skip to content

Instantly share code, notes, and snippets.

@Jursdotme
Created February 14, 2018 07:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jursdotme/68c96926abe3b377f395d702fb5c76a8 to your computer and use it in GitHub Desktop.
Save Jursdotme/68c96926abe3b377f395d702fb5c76a8 to your computer and use it in GitHub Desktop.
Mailpoet bulk subscribe
// Add Shortcode
function mailpoet_bulk_subscribe_func()
{
ob_start(); ?>
<form action="?submit=true" method="POST">
<input name="email" type="text" required placeholder="Email address">
<input name="first_name" type="text" placeholder="First name">
<input name="last_name" type="text" placeholder="Last name">
<input type="checkbox" name="lists[]" value="1"/>List 1<br/>
<input type="checkbox" name="lists[]" value="2"/>List 2<br/>
<input value="Subscribe!" type="submit">
</form>
<?php
$content = ob_get_contents();
ob_end_clean();
$slide = (isset($_GET["submit"]) && trim($_GET["submit"]) == 'true') ? trim($_GET["submit"]) : '';
if ("true" === $slide) {
$subscriber_data = array(
'email' => sanitize_text_field($_POST['email']),
'first_name' => sanitize_text_field($_POST['first_name']),
'last_name' => sanitize_text_field($_POST['first_name'])
);
$options = array(
'send_confirmation_email' => true,// default: true
'schedule_welcome_email' => false // default: true
);
$lists = array_map('intval', $_POST['lists']);
try {
$subscriber = \MailPoet\API\API::MP('v1')->addSubscriber($subscriber_data, $lists, $options);
} catch (Exception $exception) {
return $exception->getMessage();
}
}
return $content;
}
add_shortcode('mailpoet_bulk_subscribe', 'mailpoet_bulk_subscribe_func');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment