UPDATE. The new Newsletter API version 2 are available.
Newsletter provides an extensive set of functions to interface your custom code with the plugin core features. The API can be accessed programmatically as static methods of the class TNP
or via REST calls.
REST API are available installing the free Newsletter API addon (find it on Newsletter addons panel in yout blog or get it on your account page) while the TNP
class is always available in the plugin core.
General usage notes
Most API functions require a single parameter, an associative array, with required and optional values. For example to create a new subscription you’ll write:
TNP::subscribe(['email'=>'email@address.com']);
If you need to pass on the name:
TNP::subscribe(['email'=>'email@address.com', 'name'=>'The name']);
When a call generates an error, in most cases a WP_Error
object is returned with an error code and explanation. The returned values of a function should always checked in this way:
$result = TNP::something(...); if (is_wp_error($result)) { // manage the error }
When using the REST API, the parameters are regular HTTP POST key-value pairs or as a JSON object in the request raw body.
REST API calls require a key (with exceptions) which you can set on API configuration panel.
REST API responses
REST API always respond with HTTP 200 on success. The response can include data as specified for each calls. If something went wrong, you’ll get back a 4xx error. They are:
404 | Object not found |
403 | Not allowed (when the API key is missing or wrong) |
400 | Bad request, when the parameters are not correct or required parameters are missing |
Other than 4xx HTTP status codes, errors are explained in more details decoding the JSON in the response body.
Subscription process
Subscription related functions are used to start a subscription process. That means not only the subscriber’s data is added to the database, but the full subscription process starts with activation and/or welcome emails, administrator notifications and so on. If you need to manage the subscribers’ database directly, you can use the subscriber management functions (see below).
Subscribe
Starts the subscription process. If the passed email already exists the process can take different paths discussed below.
When called as REST API it can be invoked without the secret key: in this case more strict contraints are applied to the parameters (like for subscriptions coming from a public subscription form). For example if the plugin is configured to make the first name “required” calling the API without a key and without the first name will result in a error. More, for calls without an API key, private lists possibly specified will be ignored or will throw an error.
required | default | description | type | |
---|---|---|---|---|
x | a valid email address | string | ||
name | subscriber name | string | ||
surname | subscriber surname | string | ||
gender | subscriber gender | string | ||
lists (since version 5.5.0) | user lists | array | ||
profile_x | additional profile field value | string | ||
send_emails | true | Send activation and welcome emails – true or false | boolean | |
Note: if you use the subscribe
endpoint as action for an HTML form, the lists parameter must be written as lists[]
.
HTTP Request
POST /wp-json/newsletter/v1/subscribe
$ curl -X POST -H "Content-Type: application/json" http://<your-site>/wp-json/newsletter/v1/subscribe -d '{"email":"test@example.com", "name":"My name", "gender":"f"}'
Function
TNP::subscribe()
TNP::subscribe(['email'=>'test@example.com', 'name'=>'My name', 'gender'=>'f', 'lists' => [2,12]]);
Lists parameter
You have to send an array of numbers with the list ids to be assigned to the subscriber. Pre-assigned lists will be added as well.
$ curl -X POST -H "Content-Type: application/json" http://<your-site>/wp-json/newsletter/v1/subscribe -d '{"email":"test@example.com", "name":"My name", "gender":"f", "lists": [1,2,12]}'
TNP::subscribe(['email'=>'test@example.com', 'name'=>'My name', 'gender'=>'f', 'lists' => [2,12]]);
Unsubscribe
Unsubscribe the passed email address.
required | default | description | |
---|---|---|---|
x | a valid email address | ||
api_key | x | the string set on the API configuration panel |
HTTP Request
POST /wp-json/newsletter/v1/unsubscribe
$ curl -X POST -H "Content-Type: application/json" http://<your-site>/wp-json/newsletter/v1/unsubscribe -d '{"email":"test@example.com", "api_key":"..."}'
Function
TNP::unsubscribe()
TNP::unsubscribe(['email'=>'test@example.com']);
Subscribers management
This set of functions manipulates directly the subscribers database. Subscribers have an internal numeric ID which can be used to identify them and which is stable over time. But even the email address identifies uniquely a subscriber, so it can be used when updating the subscriber data. Usually, when interaction with external system is in place, the email address is used.
Add a subscriber
required | default | description | |
---|---|---|---|
x | a valid email address | ||
api_key | x | the string set on the API configuration panel | |
name | subscriber name | ||
surname | subscriber surname | ||
gender | subscriber gender | ||
profile_x | additional profile field value | ||
status | C | subscription status S = Not Confirmed C = Confirmed | |
HTTP Request
POST /wp-json/newsletter/v1/subscribers/add
$ curl -X POST -H "Content-Type: application/json" http://<your-site>/wp-json/newsletter/v1/subscribers/add -d '{"email":"test@example.com", "api_key":"..."}'
Function
TNP::add_subscriber()
TNP::add_subscriber(['email'=>'test@example.com']);
Delete a subscriber
Removes a subscriber from the database identifying it by email. The deletion is definitive.
- Endpoint:
POST /wp-json/newsletter/v1/subscribers/delete
- Function:
TNP::delete_subscriber()
required | default | description | |
---|---|---|---|
x | a subscriber email address | ||
api_key | x | the string set on the API configuration panel |
Examples
$ curl -X POST -H "Content-Type: application/json" http://<your-site>/wp-json/newsletter/v1/subscribers/delete -d '{"email":"test@example.com", "api_key":"..."}'
TNP::add_subscriber(['email'=>'test@example.com']);
Subscribers list (last 20)
required | default | description | |
---|---|---|---|
api_key | x | the string set on the API configuration panel |
HTTP Request
POST /wp-json/newsletter/v1/subscribers
$ curl -X POST -H "Content-Type: application/json" http://<your-site>/wp-json/newsletter/v1/subscribers -d '{"api_key":"..."}'
Function
TNP::subscribers()
Newsletters management
Newsletters list (last 10)
required | default | description | |
---|---|---|---|
HTTP Request
GET /wp-json/newsletter/v1/newsletters
$ curl http://<your-site>/wp-json/newsletter/v1/newsletters
Function
TNP::newsletters()