Skip to main content

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 () {
lp_add_subscriber( [ 'email' => '[email protected]' ] );
}, 20 );

Parameters​

The function takes one array:

$result = lp_add_subscriber(
[
'email' => '[email protected]',
'name' => 'Jane Doe',
'source' => 'default',
'group_id' => 12,
'send_confirmation' => null,
]
);
KeyRequiredDefaultDescription
emailYesemptyEmail address. Invalid addresses return an error
nameNoemptySubscriber name, trimmed to 100 characters
sourceNodefaultSource key for attribution. Unknown or inactive sources fall back to default. Create your own source under Sources
group_idNo0ID of an existing group. The subscriber is added to the group as well
send_confirmationNonulltrue 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():

$result = lp_add_subscriber( [ 'email' => '[email protected]' ] );

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,
'email' => '[email protected]',
'name' => 'Jane Doe',
'source' => 'default',
'status' => 'pending',
'message' => 'Subscriber added.',
]

With double opt-in off, status is subscribed and no confirmation email is sent.

KeyDescription
successAlways true
createdtrue when a subscriber row was written, including when the confirmation email failed. false when the address was already subscribed
messageHuman readable result. The created and already subscribed messages differ
email, name, sourceThe stored values
statussubscribed or pending, depending on double opt-in. Already subscribed contacts keep subscribed
confirmation_emailOnly present and set to failed when the row saved but the confirmation email could not be sent
group_id, group_addedPresent when group_id was passed. group_added is false when the contact was already a member

Error codes​

CodeMeaning
lp_invalid_emailThe email address is empty or invalid
lp_group_not_foundgroup_id does not match an existing group
lp_subscriber_not_addedThe 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.
lp_add_subscriber( [ 'email' => '[email protected]', 'send_confirmation' => true ] );

// Add the contact without a confirmation email.
lp_add_subscriber( [ 'email' => '[email protected]', 'send_confirmation' => false ] );

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​