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!!

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'];
  • Thanks but unfortunately that doesn't work either. – Philipp K Jul 4 '18 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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