Any WordPress plugin can add its own integration to OptinFlare. A third-party integration gets the same treatment as the built-in ones: a card under Integrations โ Third-party, an optional account connection with encrypted credentials, an entry in the action picker of the form editor with its own settings, a run after every successful sign-up, a line in the submission log and a re-run button on the submission screen.
This guide covers the PHP API, settings fields, account connections, the JavaScript API of the form editor and the rules OptinFlare applies when your integration runs. It builds on Hooks and filters.
Two kinds of integrations
- Subscription provider: receives the subscriber, with lists, tags, double opt-in and field mapping, like MailerPress, MailPoet or Mailchimp. Implement
OptinFlare\Providers\SubscriberProviderInterfaceand register it withoptinflare_register_providers. - Action type: does something else after a sign-up, such as posting a chat message, creating a CRM deal or calling an API. Extend
OptinFlare\Integrations\AbstractActionTypeand register it withoptinflare_register_action_types.
Both appear in the Third-party category of the Integrations screen and in the action picker of every form. OptinFlare treats an integration as third-party when its class is outside the OptinFlare\ and OptinFlarePro\ namespaces. Third-party integrations work with the free plugin; OptinFlare Pro is not required.
Requirements
- A version of OptinFlare that includes the integrations API. Check it with
interface_exists( '\OptinFlare\Integrations\ActionTypeInterface' ). - PHP 8.2 and WordPress 6.8, the same requirements as OptinFlare.
- A
Requires Plugins: optinflareline in your plugin header, so WordPress activates OptinFlare first.
Load your integration
Add your filters when your plugin loads. OptinFlare builds its list of integrations later, the first time it needs it (an admin screen, a REST request or a form submission). Load the classes that implement OptinFlare interfaces inside the callbacks, so your plugin keeps working while OptinFlare is inactive.
<?php
/**
* Plugin Name: Acme Chat for OptinFlare
* Requires Plugins: optinflare
* Requires PHP: 8.2
* Text Domain: acme-chat
*/
defined( 'ABSPATH' ) || exit;
define( 'ACME_CHAT_FILE', __FILE__ );
function acme_chat_optinflare_is_ready() {
return interface_exists( '\OptinFlare\Integrations\ActionTypeInterface' );
}
add_filter(
'optinflare_register_action_types',
function ( array $types ) {
if ( acme_chat_optinflare_is_ready() ) {
require_once __DIR__ . '/includes/class-acme-chat-action.php';
$types[] = new Acme_Chat_Action();
}
return $types;
}
);Create an action type
This action posts every new subscriber to a channel of a chat service. It declares two settings, asks for a bot token on the Integrations screen and reports the outcome in the submission log.
<?php
use OptinFlare\Integrations\AbstractActionType;
use OptinFlare\Integrations\ActionContext;
use OptinFlare\Integrations\ActionResult;
use OptinFlare\Integrations\ConnectableIntegrationInterface;
final class Acme_Chat_Action extends AbstractActionType implements ConnectableIntegrationInterface {
public function get_id(): string {
return 'acme_chat';
}
public function get_label(): string {
return __( 'Acme Chat', 'acme-chat' );
}
public function get_description(): string {
return __( 'Post each new subscriber to an Acme Chat channel.', 'acme-chat' );
}
public function get_logo_url(): string {
return plugins_url( 'assets/logo.svg', ACME_CHAT_FILE );
}
public function get_docs_url(): string {
return 'https://example.com/docs/acme-chat-for-optinflare';
}
public function get_settings_fields(): array {
return array(
array(
'key' => 'channel',
'label' => __( 'Channel', 'acme-chat' ),
'default' => '#leads',
),
array(
'key' => 'mention',
'label' => __( 'Mention the team', 'acme-chat' ),
'type' => 'toggle',
'default' => true,
),
);
}
public function get_connect_fields(): array {
return array(
array(
'key' => 'token',
'label' => __( 'Bot token', 'acme-chat' ),
'type' => 'password',
),
);
}
public function test_connection(): bool|WP_Error {
$response = wp_remote_get(
'https://api.acme.example/v1/me',
array(
'headers' => array( 'Authorization' => 'Bearer ' . ( $this->get_credentials()['token'] ?? '' ) ),
'timeout' => 10,
)
);
if ( is_wp_error( $response ) ) {
return $response;
}
return 200 === wp_remote_retrieve_response_code( $response )
? true
: new WP_Error( 'acme_chat_auth', __( 'Acme Chat rejected the token.', 'acme-chat' ) );
}
public function run( array $settings, ActionContext $context ): ActionResult {
$text = sprintf(
'%s%s subscribed through "%s".',
$settings['mention'] ? '@team ' : '',
$context->email,
$context->form_title
);
$response = wp_remote_post(
'https://api.acme.example/v1/messages',
array(
'headers' => array(
'Authorization' => 'Bearer ' . ( $this->get_credentials()['token'] ?? '' ),
'Content-Type' => 'application/json',
),
'body' => wp_json_encode(
array(
'channel' => $settings['channel'],
'text' => $text,
)
),
'timeout' => 10,
)
);
if ( is_wp_error( $response ) ) {
return ActionResult::failure( $response );
}
if ( 300 <= wp_remote_retrieve_response_code( $response ) ) {
return ActionResult::failure( __( 'Acme Chat did not accept the message.', 'acme-chat' ) );
}
/* translators: %s: chat channel. */
return ActionResult::success( sprintf( __( 'Posted to %s.', 'acme-chat' ), $settings['channel'] ) );
}
}Once your plugin is active, Acme Chat appears under Integrations โ Third-party with Connect, Test connection and Disconnect. After the connection, editors can add it from the action picker of any form; the two declared settings are shown in its panel.
Action type methods
get_id(): string: stable identifier stored with each form action. Use lowercase letters, digits, dashes and underscores, with a prefix of your own. It must differ from every provider and fromdefault,internal,email_notificationandwebhook. An invalid or duplicate identifier is ignored and reported with_doing_it_wrong().get_label(): stringandget_description(): string: shown in the action picker, on the Integrations screen and in the submission log.run( array $settings, ActionContext $context ): ActionResult: runs the action. Required.is_available(): bool: an unavailable action is locked in the editor and is not run. Default:true, or โconnectedโ for a connectable action type.get_capability(): string: capability required to add, change or remove the action on a form, the way webhooks requireoptinflare_use_webhooks. Default:'', every form editor.get_settings_fields(): array: settings rendered in the action panel. Default: none.get_defaults(): array: settings of a new action. Default: thedefaultvalue of each field.sanitize_settings( array $settings ): array: sanitizes the settings when the form is saved. Default: by field type (see below).get_logo_url(): stringandget_docs_url(): string: logo and Documentation link of the integration card. Default: generic icon, no link.
AbstractActionType implements ActionTypeInterface and IntegrationMetadataInterface. You can implement ActionTypeInterface directly instead; every method above is then yours to write.
Settings fields
Fields returned by get_settings_fields() are rendered in the action panel, applied to new actions and sanitized when the form is saved. Each field is an array with these keys:
key(required): setting name, in lowercase.label(required): field label.type:text(default),textarea,email,url,password,number,selectortoggle.helpandplaceholder.default: value of a new action, and of a setting missing from an older action.options: for a select, a list ofarray( 'value' => 'a', 'label' => 'A' ).
The default sanitization keeps declared fields only: sanitize_text_field() for text and password, sanitize_textarea_field() for textarea, sanitize_email() on each comma-separated address, esc_url_raw() for url, a number for number, one of the options for select and a boolean for toggle. An action type without declared fields gets a generic sanitization instead: tags are stripped from strings, booleans and numbers are kept and anything else is dropped.
To keep HTML, nested data or settings stored by your own editor component, override sanitize_settings():
public function sanitize_settings( array $settings ): array {
$sanitized = parent::sanitize_settings( $settings );
if ( isset( $settings['message'] ) && is_string( $settings['message'] ) ) {
$sanitized['message'] = wp_kses_post( $settings['message'] );
}
return $sanitized;
}OptinFlare manages these keys on every action: id, type, enabled, action_kind, audiences, tags, double_optin, field_mapping, subscription_groups. Your integration never receives them and cannot declare them. Settings are stored in clear with the form: keep passwords and API keys in connect fields, which are encrypted.
Run context and result
run() receives the sanitized settings of the action and an ActionContext with these read-only properties:
form_idandform_title.email,first_nameandlast_name.custom_fields: the other submitted fields, keyed by field key.lang: the visitor’s language.entry: the full submission payload (event,form,subscriber,submitted_at,site), identical to the webhook payload.is_retry:truewhen an administrator re-runs the action from the submission screen.
Return one of these results:
ActionResult::success( $message = '', $contact_id = 0 ): the action completed. The message appears in the submission log; without it, OptinFlare writes โLabel completed.โActionResult::queued( $message = '' ): the work was accepted and continues in the background. The log shows it as queued.ActionResult::failure( $error ): the action failed, with a message or aWP_Error.
An exception thrown by run() is recorded as a failure, and the other actions of the form still run. Actions run while the visitor waits for the form response: use short HTTP timeouts, and move slow work to WP-Cron or Action Scheduler before returning ActionResult::queued().
Connect an account
Implement OptinFlare\Integrations\ConnectableIntegrationInterface when your service needs credentials:
get_connect_fields(): array: fields of the connection dialog, each withkey,label,type(textorpassword),placeholderandrequired(trueby default).test_connection(): true|WP_Error: checks the stored credentials.
When an administrator clicks Connect, OptinFlare encrypts and stores the values, calls test_connection(), and deletes them again if the test fails, showing your error message. Read them with $this->get_credentials() or OptinFlare\Providers\Credentials::get( 'acme_chat' ). With AbstractActionType, the action stays unavailable until the account is connected.
Create a subscription provider
A provider receives the subscriber instead of running a task. This one creates a lead in a CRM plugin installed on the same site and uses its pipelines as lists:
<?php
use OptinFlare\Integrations\IntegrationMetadataInterface;
use OptinFlare\Providers\SubscriberProviderInterface;
use OptinFlare\Providers\SubscriptionRequest;
use OptinFlare\Providers\SubscriptionResult;
// phpcs:disable WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid -- Names imposed by SubscriberProviderInterface.
final class Acme_Crm_Provider implements SubscriberProviderInterface, IntegrationMetadataInterface {
public function getId(): string {
return 'acme_crm';
}
public function getLabel(): string {
return 'Acme CRM';
}
public function getKind(): string {
return self::KIND_LOCAL;
}
public function getDescription(): string {
return __( 'Create a lead in Acme CRM for every subscriber.', 'acme-crm' );
}
public function isAvailable(): bool {
return function_exists( 'acme_crm_create_lead' );
}
public function getAudiences(): array {
return array_map(
static fn( $pipeline ) => array(
'id' => $pipeline->id,
'name' => $pipeline->name,
),
acme_crm_get_pipelines()
);
}
public function getTags(): array {
return array();
}
public function getFields(): array {
return array();
}
public function supports( string $feature ): bool {
return false;
}
public function subscribe( SubscriptionRequest $request ): SubscriptionResult {
$lead_id = acme_crm_create_lead(
array(
'email' => $request->email,
'first_name' => $request->firstName,
'last_name' => $request->lastName,
'pipelines' => $request->audiences,
)
);
if ( is_wp_error( $lead_id ) ) {
return SubscriptionResult::failure( $lead_id );
}
return SubscriptionResult::ok( (int) $lead_id, array() );
}
public function get_logo_url(): string {
return plugins_url( 'assets/logo.svg', ACME_CRM_FILE );
}
public function get_docs_url(): string {
return '';
}
}Register it the same way, with optinflare_register_providers:
add_filter(
'optinflare_register_providers',
function ( array $providers ) {
if ( interface_exists( '\OptinFlare\Integrations\IntegrationMetadataInterface' ) ) {
require_once __DIR__ . '/includes/class-acme-crm-provider.php';
$providers[] = new Acme_Crm_Provider();
}
return $providers;
}
);Provider methods
getId(),getLabel()andgetDescription(): identity of the provider, with the same rules as an action type identifier.getKind():KIND_LOCALfor a plugin on the same site (the card offers Install plugin while it is unavailable) orKIND_REMOTEfor a service connected with an API key (the card offers Connect).isAvailable(): whether subscriptions can be sent right now.getAudiences()andgetTags(): lists ofarray( 'id' => โฆ, 'name' => โฆ )offered in the editor.getFields(): custom fields asarray( 'field_key' => โฆ, 'label' => โฆ, 'type' => โฆ )for field mapping. Return an empty array when unsupported.supports( string $feature ):FEATURE_TAGS,FEATURE_DOUBLE_OPTIN,FEATURE_CUSTOM_FIELDSorFEATURE_AUTOMATIONS. It decides which settings the editor shows: tags, the double opt-in switch and field mapping.subscribe( SubscriptionRequest $request ): SubscriptionResult: sends the subscriber.
SubscriptionRequest provides email, firstName, lastName, audiences, tags, customFields, doubleOptin, formId, formTitle, lang, subscriptionGroups, action_settings. When the form maps fields, customFields uses your field keys.
Return SubscriptionResult::ok( $contact_id, $data ) or SubscriptionResult::failure( $error ). If your service decides the opt-in mode itself, report what it applied in $data with the double_optin_applied and pending keys: OptinFlare records them in the consent proof and tells the visitor to confirm their address.
Provider methods use camelCase because the interface predates the integrations API. If you follow the WordPress coding standards, keep the phpcs:disable comment shown above, and add an ignore comment where you read $request->firstName or $request->lastName.
Optional provider interfaces
OptinFlare\Integrations\IntegrationMetadataInterface: logo and documentation link.OptinFlare\Integrations\ActionSettingsInterface:get_settings_fields()andsanitize_settings()add your own fields below lists, tags and double opt-in. Read them insubscribe()from$request->action_settings.OptinFlare\Integrations\SettingsFields::sanitize( $fields, $settings )gives you the field-based sanitization.OptinFlare\Providers\ConnectableProviderInterface:getConnectFields(),testConnection()andflushAudienceCache()for a remote service connected with an API key.OptinFlare\Providers\SubscriptionGroupsProviderInterface: subscription groups chosen per form.
Customize the form editor with JavaScript
Declared fields cover most needs. For a custom interface, such as a preview, a dynamic list or a note about your own settings, register a component for your action type or provider identifier. Enqueue your script on optinflare_enqueue_admin_assets with optinflare as a dependency, so that window.optinFlare.integrations exists when it runs:
add_action(
'optinflare_enqueue_admin_assets',
function () {
wp_enqueue_script(
'acme-chat-optinflare',
plugins_url( 'assets/optinflare-editor.js', ACME_CHAT_FILE ),
array( 'optinflare', 'wp-element', 'wp-components', 'wp-i18n' ),
'1.0.0',
true
);
wp_set_script_translations( 'acme-chat-optinflare', 'acme-chat' );
}
);( function ( wp ) {
const el = wp.element.createElement;
const { __ } = wp.i18n;
window.optinFlare.integrations.registerActionSettings( 'acme_chat', {
// Rendered after the declared fields (and, for a provider, after lists, tags and double opt-in).
edit: ( { action, onChange, definition, formFields } ) =>
el( wp.components.TextControl, {
label: __( 'Message prefix', 'acme-chat' ),
value: action.prefix || '',
onChange: ( prefix ) => onChange( { ...action, prefix } ),
__next40pxDefaultSize: true,
__nextHasNoMarginBottom: true,
} ),
// Line under the action name in the actions list.
summary: ( action ) => action.channel || '',
// Dashicon name or @wordpress/icons icon.
icon: 'format-chat',
} );
} )( window.wp );The component receives these props:
action: the form action, including your settings.onChange( nextAction ): saves the action. Always spread the current action, as in{ ...action, prefix }.definition: what PHP sent for the integration: label, availability,settingsFields,defaultsโฆformFields: the fields of the edited form, each with akeyand alabel.
registerActionSettings( id, settings ) returns false for an invalid identifier; unregisterActionSettings( id ) and getActionSettings( id ) complete the API. An error in your component shows a notice in the panel without breaking the editor, and OptinFlare Pro uses the same API in its actions workspace.
Settings stored by your component are saved with the form and passed to run(), or to $request->action_settings for a provider. Sanitize them in sanitize_settings(): when an action type declares fields, the default sanitization keeps those fields only, so the prefix above needs its own line:
public function sanitize_settings( array $settings ): array {
$sanitized = parent::sanitize_settings( $settings );
$sanitized['prefix'] = sanitize_text_field( (string) ( $settings['prefix'] ?? '' ) );
return $sanitized;
}How OptinFlare runs your integration
- Actions run in the order set in the form, after the sign-up passes validation and the spam and CAPTCHA checks. Forms that are not published never run them.
- Each run adds a line to the submission log, with your label and message. Administrators can re-run it from the submission screen:
$context->is_retryis thentrueand the hooks are not fired again. optinflare_action_ran( $type, null, $form_id )fires after an action type runs.- An unavailable action is not run and is logged as unavailable.
- If your plugin is deactivated, its actions stay in the forms and show as unavailable until it comes back. OptinFlare never replaces them with a subscription to the default provider.
- A user without the capability returned by
get_capability()cannot add, change or remove the action: saving the form is refused.
REST API
GET /wp-json/optinflare/v1/providersreturns each provider withcategory,logo,docsUrlandsettingsFields, and the action types inactionTypes, withid,label,description,kind(action),category,available,allowed,connectFields,settingsFieldsanddefaults.POSTandDELETE /wp-json/optinflare/v1/providers/{id}/connectandPOST /wp-json/optinflare/v1/providers/{id}/testalso accept the identifier of a connectable action type.
Checklist before release
- A prefixed, stable identifier: it is stored in every form that uses your integration, so never rename it.
Requires Plugins: optinflarein your header and aninterface_exists()check before loading your classes.- Labels and descriptions translated with your own text domain.
- Short HTTP timeouts, and slow work moved to the background.
- Every setting stored by your component sanitized in
sanitize_settings(). - Secrets in connect fields, never in settings fields.
- A full test: connect the account, add the action to a published form, submit it, check the Submissions screen, re-run the action, then deactivate your plugin and submit again.