最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

plugins - Mailpoet Sign up on Custom registration form won't work

programmeradmin0浏览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!!

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

Share Improve this question edited Jul 4, 2018 at 15:00 Philipp K asked Jul 4, 2018 at 14:53 Philipp KPhilipp K 1931 gold badge3 silver badges10 bronze badges
Add a comment  | 

2 Answers 2

Reset to default 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'];

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);
发布评论

评论列表(0)

  1. 暂无评论