Title: Add Subscriber with PHP
Last modified: October 17, 2017

---

# Add Subscriber with PHP

 *  [nfr816](https://wordpress.org/support/users/nfr816/)
 * (@nfr816)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add-subscriber-with-php/)
 * Hello –
 * Is it possible to add a subscriber to a list just using PHP? Using Mailpoet v3
   on WordPress.
 * Regards,
 * Neil.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Wysija](https://wordpress.org/support/users/wysija/)
 * (@wysija)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add-subscriber-with-php/#post-9596177)
 * Hi,
 * It sure is possible [via our API](http://beta.docs.mailpoet.com/article/195-add-subscribers-through-your-own-form-or-plugin).
 * Good luck!
 *  Thread Starter [nfr816](https://wordpress.org/support/users/nfr816/)
 * (@nfr816)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add-subscriber-with-php/#post-9596213)
 * Hello –
 * I have tried the following code but I cannot get it to work. Could you please
   point me in the right direction?
 * <?php
 * include ‘/path/to/wp-content/plugins/mailpoet/lib/API/API.php’;
 * //$subscriber_data = array(
    // ’email’ => ‘myemail@mydomain.com’, // ‘first_name’
   => ‘MyFirstName’, // ‘last_name’ => ‘MyLastName’ // ’email’ => sanitize_text_field(
   $_POST[’email’]), // ‘first_name’ => sanitize_text_field($_POST[‘first_name’]),//‘
   last_name’ => sanitize_text_field($_POST[‘first_name’]) //);
 * $subscriber = “myemail@mydomain.com”; // you can also pass a subscriber ID
    $
   list = 2; // you can get available list IDs by using the \MailPoet\API\API::MP(‘
   v1’)->getLists(); method
 * try {
    $subscriber = \MailPoet\API\API::MP(‘v1’)->subscribeToList($subscriber,
   $list); } catch(Exception $exception) { return $exception->getMessage(); echo“
   failed”; } ?>
 * Many thanks,
 * Neil.
    -  This reply was modified 8 years, 7 months ago by [nfr816](https://wordpress.org/support/users/nfr816/).
      Reason: Code update
 *  [Wysija](https://wordpress.org/support/users/wysija/)
 * (@wysija)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add-subscriber-with-php/#post-9596440)
 * Re,
 * Is this a standalone code or is it loaded by WordPress? In case of the former,
   it won’t work – the plugin needs to be fully initialized.
 * MailPoet Team.
 *  Thread Starter [nfr816](https://wordpress.org/support/users/nfr816/)
 * (@nfr816)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add-subscriber-with-php/#post-9596481)
 * Hello –
 * It is standalone php code, so I suppose it will not work…
 * In that case, what are the mysql database actions performed by the API when a
   new subscriber is added to a list?
 * I can see:
 * INSERT into mailpoet_subscribers
    INSERT into mailpoet_subscriber_segment (INSERT
   into mailpoet_subscriber_custom_field)
 * Which mysql tables do I need to update in order to ensure that the user is sent
   a “Welcome” message when he / she is subscribed to a list?
 * Best regards,
 * Neil.
 *  [xberg](https://wordpress.org/support/users/xberg/)
 * (@xberg)
 * [8 years, 7 months ago](https://wordpress.org/support/topic/add-subscriber-with-php/#post-9598114)
 * Neil,
    You’re one step ahead of me. I would not recommend trying to do direct
   database inserts: I have done this in the past for mailpoet V2 but it’s a nuisance
   to maintain.
 * What the plugin author was hinting is that you did not initialise mailpoet.
    
   Perhaps doing this at the beginning of your PHP will help: require_once( “../
   wp-load.php” ); of course wherever you wp-load.php is actually situated.
 *  Thread Starter [nfr816](https://wordpress.org/support/users/nfr816/)
 * (@nfr816)
 * [8 years, 6 months ago](https://wordpress.org/support/topic/add-subscriber-with-php/#post-9619208)
 * Hello –
 * I have the following but it is not working correctly:
 * <?php
 * require_once( “/path/to/httpdocs/wp-load.php” );
 * $mp_subscriber_data = array(
    ’email’ => ‘test@example.com’, ‘first_name’ => ‘
   John’, ‘last_name’ => ‘Doe’, );
 * $mp_list = 2; // you can get available list IDs by using the \MailPoet\API\API::
   MP(‘v1’)->getLists(); method
 * $mp_options = array(
    // default: true “send_confirmation_email” => “false”, //
   default: true “schedule_welcome_email” => “false”, );
 * try {
    $mp_subscriber = \MailPoet\API\API::MP(‘v1’)->subscribeToList($mp_subscriber_data,
   $mp_list, $mp_options); //$mp_subscriber = \MailPoet\API\API::MP(‘v1’)->subscribeToList(
   $mp_subscriber, $mp_list); } catch(Exception $exception) { return $exception-
   >getMessage(); } ?>
 * I have the following error:
 * PHP Warning: trim() expects parameter 1 to be string, array given in /path/to/
   httpdocs/wp-content/plugins/mailpoet/lib/Models/Subscriber.php on line 31
 * Running it on the commnd line with:
 * php script_name.php
 * Any ideas on how I can resolve this?
 * Regards,
 * Neil.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Add Subscriber with PHP’ is closed to new replies.

 * ![](https://ps.w.org/mailpoet/assets/icon-256x256.png?rev=3284564)
 * [MailPoet - Newsletters, Email Marketing, and Automation](https://wordpress.org/plugins/mailpoet/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/mailpoet/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/mailpoet/)
 * [Active Topics](https://wordpress.org/support/plugin/mailpoet/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/mailpoet/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/mailpoet/reviews/)

## Tags

 * [Mailpoet 3](https://wordpress.org/support/topic-tag/mailpoet-3/)
 * [php](https://wordpress.org/support/topic-tag/php/)

 * 6 replies
 * 3 participants
 * Last reply from: [nfr816](https://wordpress.org/support/users/nfr816/)
 * Last activity: [8 years, 6 months ago](https://wordpress.org/support/topic/add-subscriber-with-php/#post-9619208)
 * Status: not resolved