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.
-
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(); methodtry {
$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. Reason: Code update
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.
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.
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.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.
-
This reply was modified 8 years, 7 months ago by
The topic ‘Add Subscriber with PHP’ is closed to new replies.