0

I've been trying to solve this myself for hours but I can't figure out where I am going wrong.

I have a custom registration form on my website and I would like to add a checkbox for users to sign up for the newsletter.

I went through the Mailpoet Docs and tried everything that they describe there but something is not working for me.

The user is being signed up successfully but never gets added to my mainling list. Here's my code

function newsletterSignUp(){
    $subscriber_data = array(
      'email' => sanitize_text_field($_POST['email']),
      'first_name' => sanitize_text_field($_POST['first_name']),
      'last_name' => sanitize_text_field($_POST['last_name'])
    );

    $list = \MailPoet\API\API::MP('v1')->getLists()[0][id];
    //Returns the ID of the list that I want to assign subscribers to 

    try {
      $subscriber = \MailPoet\API\API::MP('v1')->addSubscriber($subscriber_data, $list, $options);
    } catch(Exception $exception) {
      // return $exception->getMessage();
    }
}

I am calling this function like this:

if(isset($_POST['submit'])){
// Some other code to sign up the user

   if(isset($_POST['subscribe'])){  //The name of my checkbox input
     newsletterSignUp();
   }
}

Any help will be greatly appreciated!!

2 Answers 2

0

On this line here:

$list = \MailPoet\API\API::MP('v1')->getLists()[0][id];

You're missing quotes around id, so $list isn't being set to the ID. You need to add quotes:

$list = \MailPoet\API\API::MP('v1')->getLists()[0]['id'];
1
  • Thanks but unfortunately that doesn't work either.
    – Philipp K
    Jul 4, 2018 at 14:59
0

This is an old question, but as I’ve been dealing with the same general problem today and turned this up in the process of resolving my issue, I thought it would be worthwhile to make an observation that may help others to proceed.

Besides the omission of quotes noted by Jacob Peattie, I see a problem with the second (optional) parameter in addSubscriber(). That second parameter needs to be an array — see Mailpoet’s documentation on this. Even if only adding the user to a single list, the parameter should be an array (a one-element array in this case).

So instead of

$list = \MailPoet\API\API::MP('v1')->getLists()[0]['id'];

and

$subscriber = \MailPoet\API\API::MP('v1')->addSubscriber($subscriber_data, $list, $options);

it should be something like

$list = \MailPoet\API\API::MP('v1')->getLists()[0]['id'];
$list_ids = array( $list );

with

$subscriber = \MailPoet\API\API::MP('v1')->addSubscriber($subscriber_data, $list_ids, $options);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.