Add a Subscriber
lp_add_subscriber() adds an email address to your audience with one call. Use it from a custom plugin, a theme's functions file, a form handler, a checkout hook, or an import script. The subscriber passes through the same checks as public signups: blocklist, duplicate handling, and your double opt-in setting. The built-in subscribe forms call this same function, so custom code and the public form behave identically.
When to call
Call the function after LetterPress has loaded, for example on plugins_loaded with priority 20 or later, or later in the request such as a form handler.
add_action( 'plugins_loaded', function () {
}, 20 );
Parameters
The function takes one array:
$result = lp_add_subscriber(
[
'name' => 'Jane Doe',
'source' => 'default',
'group_id' => 12,
'send_confirmation' => null,
]
);
| Key | Required | Default | Description |
|---|---|---|---|
email | Yes | empty | Email address. Invalid addresses return an error |
name | No | empty | Subscriber name, trimmed to 100 characters |
source | No | default | Source key for attribution. Unknown or inactive sources fall back to default. Create your own source under Sources |
group_id | No | 0 | ID of an existing group. The subscriber is added to the group as well |
send_confirmation | No | null | true forces the confirmation email, false skips it. Leave unset to follow the double opt-in setting |
Return values
Success returns an array, including when the address was already subscribed. Failure returns a WP_Error. Always check with is_wp_error():
if ( is_wp_error( $result ) ) {
// $result->get_error_code() and get_error_message() explain the failure.
return;
}
if ( $result['created'] ) {
// New row written. Check confirmation_email if it is present.
} else {
// Already subscribed. The message says so, and only a passed group changed.
}
With double opt-in enabled, a successful call returns:
[
'success' => true,
'created' => true,
'name' => 'Jane Doe',
'source' => 'default',
'status' => 'pending',
'message' => 'Subscriber added.',
]
With double opt-in off, status is subscribed and no confirmation email is sent.
| Key | Description |
|---|---|
success | Always true |
created | true when a subscriber row was written, including when the confirmation email failed. false when the address was already subscribed |
message | Human readable result. The created and already subscribed messages differ |
email, name, source | The stored values |
status | subscribed or pending, depending on double opt-in. Already subscribed contacts keep subscribed |
confirmation_email | Only present and set to failed when the row saved but the confirmation email could not be sent |
group_id, group_added | Present when group_id was passed. group_added is false when the contact was already a member |
Error codes
| Code | Meaning |
|---|---|
lp_invalid_email | The email address is empty or invalid |
lp_group_not_found | group_id does not match an existing group |
lp_subscriber_not_added | The address is blocklisted or blocked, for example after a bounce or complaint |
Controlling the confirmation email
By default the function follows the double opt-in setting to decide whether a confirmation email goes out and whether the contact starts as pending. Pass send_confirmation to override that for a single call:
// Force a confirmation email even when double opt-in is off.
// Add the contact without a confirmation email.
With false the subscriber is stored as subscribed immediately. Use it when consent was already collected elsewhere, such as your own opt-in checkbox.
Examples
Add a customer after checkout
add_action( 'woocommerce_thankyou', function ( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order || ! $order->get_billing_email() ) {
return;
}
lp_add_subscriber(
[
'email' => $order->get_billing_email(),
'name' => trim( $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() ),
'source' => 'checkout', // A source you created under Sources.
]
);
} );
PHP or REST API
Use this function when your code runs inside WordPress. Use the REST endpoint when the caller is outside WordPress, such as a landing page on another domain or a script. See REST API Keys for the external route.
Next steps
- Manage group membership by email with the Group Membership API.
- Segment contacts and create sources in Groups and Sources.