Developer API
WP Smart Consent is built so other plugins can extend it. There are two ways to work with it:
- The Developer API: the recommended, stable way to send opted-in contacts and register your own integration.
- Hooks and filters: lower-level actions and filters for fine-grained control over the opt-in pipeline and each destination.
Use the API when you want another plugin (a form, a checkout, a custom flow) to feed opted-in contacts into WP Smart Consent. Your plugin hands over a contact; WP Smart Consent logs it and forwards it to every connected CRM and webhook. You never talk to a CRM directly.
Forward an opt-in#
wpsc_forward_optin( array $args ): bool
Logs the submission and, when consent is given, forwards the contact to every active destination. Returns true if accepted (valid email), false otherwise.
php
wpsc_forward_optin( [
'email' => 'jane@example.com', // required
'first_name' => 'Jane',
'last_name' => 'Doe',
'phone' => '+1 555 0100',
'company' => 'Acme Inc',
'country' => 'US',
'consent' => true, // false = log only, do not forward
'source' => 'my-plugin', // shown in the dashboard activity log
'extra' => [ // optional custom field key/value pairs
'plan' => 'pro',
],
'crm_overrides' => [ // optional per-CRM list/tag overrides
'mailchimp' => [
'list_ids' => [ 'abc123' ],
'tags' => [ 'lead' ],
],
],
] );
Accepted keys: email (required), first_name, last_name, phone, company, country, city, state, address_1, address_2, postcode, consent (bool, default true), source, extra (array), crm_overrides (array keyed by CRM slug). All values are sanitized.
The same call is available as an action, useful inside your own hooks:
php
do_action( 'wpsc_forward_optin', [
'email' => $email,
'consent' => $checkbox_was_ticked,
'source' => 'my-plugin',
] );
Register an integration#
wpsc_register_integration( array $args ): bool
Makes your integration appear in WP Smart Consent → Form integrations with its own enable toggle, and adds it to the activity-log source filter. The toggle is stored like every native integration.
php
add_action( 'init', function () {
if ( function_exists( 'wpsc_register_integration' ) ) {
wpsc_register_integration( [
'slug' => 'my-plugin', // required, unique (a-z, 0-9, _, -)
'name' => 'My Plugin', // required, display name
'group' => 'sources', // 'sources' (forms) or 'checkout'
'logo' => 'https://…/logo.png', // optional
] );
}
} );
You can also register through a filter, which fits WP Smart Consent’s load order:
php
add_filter( 'wpsc_register_integrations', function ( $integrations ) {
$integrations[] = [ 'slug' => 'my-plugin', 'name' => 'My Plugin' ];
return $integrations;
} );
Respect the site owner’s toggle#
Check whether the owner enabled your integration before forwarding:
php
$enabled = \WPSmartConsent\Settings\Settings::get_integration_option( 'my-plugin', 'enabled' );
if ( $enabled ) {
wpsc_forward_optin( [ /* … */ 'source' => 'my-plugin' ] );
}
Full example#
php
add_action( 'init', function () {
if ( function_exists( 'wpsc_register_integration' ) ) {
wpsc_register_integration( [
'slug' => 'my-plugin',
'name' => 'My Plugin',
'group' => 'sources',
] );
}
} );
add_action( 'my_plugin/form_submitted', function ( $entry ) {
if ( ! function_exists( 'wpsc_forward_optin' ) ) {
return; // WP Smart Consent not active
}
$enabled = \WPSmartConsent\Settings\Settings::get_integration_option( 'my-plugin', 'enabled' );
if ( ! $enabled ) {
return;
}
wpsc_forward_optin( [
'email' => $entry['email'],
'first_name' => $entry['name'],
'consent' => ! empty( $entry['marketing_optin'] ),
'source' => 'my-plugin',
] );
} );
Always guard API calls with function_exists() so your plugin degrades gracefully when WP Smart Consent isn’t installed. Third-party slugs cannot override native integration slugs.
Hooks#
Action: wpsc_forward_optin#
The action form of the submit API (see above). Fire it with the same $args array wpsc_forward_optin() accepts.
php
do_action( 'wpsc_forward_optin', $args );
Action: wpsc_optin_trigger#
The core low-level action every built-in destination listens on. The API fires this for you after logging; use it directly only if you need to bypass logging or build your own destination.
php
do_action( 'wpsc_optin_trigger', $optin, $order, $billing_address, $context );
$optin(bool) — whether consent was given.$order(mixed|null) — the order object for checkout sources, ornull.$billing_address(array) — contact data:email,first_name,last_name,phone,company,country,city,state,address_1,address_2,postcode, plus any custom keys.$context(array) — optional, may carrycrm_overrides({ slug: { list_ids[], tags[] } }) and other per-request data.
Build your own destination by listening on it:
php
<pre><code class="language-js">
add_action( 'wpsc_optin_trigger', function ( $optin, $order, $billing, $context = [] ) {
if ( ! $optin ) {
return;
}
// Send $billing['email'] wherever you like.
}, 10, 4 );
</code></pre>
Filters#
wpsc_register_integrations#
Register integrations during normal load (array in, array out). See the API section.
Per-CRM list IDs#
Override the target list/audience IDs for a destination at runtime. Named wpsc_{provider}_list_ids:
wpsc_mailchimp_list_ids, wpsc_brevo_list_ids, wpsc_kit_list_ids, wpsc_klaviyo_list_ids, wpsc_activecampaign_list_ids, wpsc_mailerlite_list_ids, wpsc_getresponse_list_ids, wpsc_moosend_list_ids, wpsc_mailjet_list_ids, wpsc_drip_list_ids, wpsc_campaigner_list_ids, wpsc_newsletter_list_ids, wpsc_mailster_list_ids.
php
<pre><code class="language-js">
add_filter( 'wpsc_mailchimp_list_ids', function ( $list_ids, $context ) {
return [ 'your-list-id' ];
}, 10, 2 );
</code></pre>
Contact data filters#
Adjust the payload sent to a specific destination:
wpsc_fluentcrm_contact_datawpsc_mailerpress_contact_datawpsc_mailster_subscriber_datawpsc_newsletter_user_datawpsc_groundhogg_contact_fields
Subscribe options / status#
wpsc_mailpoet_subscribe_options
Webhooks#
wpsc_webhook_data_before_send— modify the JSON body before a webhook fires.wpsc_webhook_headers— add or change request headers.
Geo / GDPR#
wpsc_gdpr_countries— customize the list of countries treated as requiring explicit consent.
Custom field values#
wpsc_field_{slug}— supply a value for a custom data field at send time.
Admin#
wpsc_menu_name— change the admin menu label.
Notes#
- Guard all API calls with
function_exists(). - Contacts forwarded through the API respect the global logging setting, the owner’s consent rules, and only reach the CRMs the owner has enabled.
- The
wpsc_forward_optin()API is the recommended entry point; thewpsc_optin_triggeraction is the lower-level primitive it’s built on.