SwiftAction is extended with plain PHP functions and WordPress hooks. The built-in WooCommerce and EDD integrations use the same API described here. IDs are lowercase and namespaced with a dot, for example myplugin.ticket_closed.
Start workflows from your code
Fire a custom event
Workflows using the Custom event trigger with the same event name start. No registration needed.
swiftaction_fire_event( 'lead_captured', array(
'email' => $email, // identifies the contact (or pass $who as the 3rd argument)
'source' => 'webinar', // any data: {{trigger.source}}, conditions
) );$who can be a user ID, an email, a WP_User or a Contact. By default SwiftAction uses $data['user_id'], then $data['email'], then the current visitor. Admins can also use the WordPress hook trigger to listen to any plugin’s action hook without code.
Enroll a contact directly
$run = swiftaction_enroll_contact( $workflow_id, $user_id_or_contact, array( 'plan' => 'pro' ), $force = false );
if ( is_wp_error( $run ) ) {
// Not active, blocked by the re-entry rule, ...
}Where to register
add_action( 'swiftaction_register', function ( \SwiftAction\Plugin $plugin ) {
// swiftaction_register_trigger(), swiftaction_register_action(), swiftaction_register_condition_field() ...
} );swiftaction_register runs on init (priority 5), after the built-in integrations load.
Custom triggers
swiftaction_register_trigger( 'myplugin.ticket_closed', array(
'label' => __( 'Support ticket closed', 'myplugin' ),
'description' => __( 'A customer’s support ticket is closed.', 'myplugin' ),
'group' => __( 'Support', 'myplugin' ),
'icon' => 'tickets-alt', // Dashicon name
'fields' => array( // settings on the trigger step
array( 'key' => 'priority', 'type' => 'select', 'label' => 'Priority',
'options' => array( '' => 'Any', 'high' => 'High', 'low' => 'Low' ) ),
),
'provides' => array( 'ticket_id' => 'Ticket ID', 'priority' => 'Priority' ),
// Only called while an active workflow uses this trigger:
'listen' => function ( callable $fire ) {
add_action( 'myplugin_ticket_closed', function ( $ticket ) use ( $fire ) {
$contact = swiftaction_get_user_contact( (int) $ticket->user_id );
$fire( $contact, array( 'ticket_id' => $ticket->id, 'priority' => $ticket->priority ) );
} );
},
// Per workflow: does this event match the step's settings?
'matches' => fn( array $s, array $data ) => '' === ( $s['priority'] ?? '' ) || $s['priority'] === $data['priority'],
) );- Pass
nullas the contact when there’s no known person; no run starts. - Keep
$datasmall and scalar (IDs, not objects): it’s stored with the run. - Re-entry rules, loop protection, pausing and versions are handled for you.
Custom actions
swiftaction_register_action( 'myplugin.create_ticket', array(
'label' => __( 'Create support ticket', 'myplugin' ),
'group' => __( 'Support', 'myplugin' ),
'fields' => array(
array( 'key' => 'subject', 'type' => 'text', 'label' => 'Subject', 'required' => true, 'merge_tags' => true ),
),
'retry' => array( 'attempts' => 3, 'backoff' => 60 ), // optional: retry WP_Errors after 60s, 120s, 240s
'handler' => function ( array $settings, \SwiftAction\Engine\RunContext $context ) {
// $settings has defaults applied and merge tags rendered.
$id = myplugin_create_ticket( $context->contact->email, $settings['subject'] );
if ( ! $id ) {
return new WP_Error( 'ticket_failed', 'Could not create the ticket.' ); // logged, retried
}
return array(
'ticket_id' => $id, // shown in Replay
'context' => array( 'ticket_id' => $id ), // {{trigger.ticket_id}} in later steps
);
// Or: return array( 'skipped' => true, 'reason' => 'No email address' );
},
'dry_run' => fn( array $s ) => sprintf( 'Would create a ticket "%s"', $s['subject'] ), // shown by Simulate
) );RunContext gives you ->contact, ->workflow, ->run, ->dry_run, ->trigger(), ->data(), ->get( $key, $default ) and ->user().
Custom condition fields
swiftaction_register_condition_field( 'myplugin.open_tickets', array(
'label' => __( 'Open support tickets', 'myplugin' ),
'group' => __( 'Support', 'myplugin' ),
'type' => 'number', // text | number | select | multiselect | boolean | date (Unix timestamp)
'resolve' => fn( \SwiftAction\Engine\RunContext $c ) => myplugin_count_open( $c->contact->email ),
// 'options' => array or callable (value => label) for select/multiselect
// 'triggers' => array( 'myplugin.ticket_closed' ) // only offer it for these triggers
) );Settings field types
Used by fields on triggers and actions; the builder renders the form automatically.
| Key | Meaning |
|---|---|
type | text, textarea, richtext, number, email, url, select, multiselect, checkbox, date, datetime, duration, tags, user_roles, key_value, image, hidden |
label, help, placeholder, default, required | As expected. |
options | value => label array, or a callable returning one. |
merge_tags | true to allow merge tags (rendered before your handler runs); 'json' to JSON-escape values. |
show_if | array( 'field' => 'mode', 'value' => 'custom' ); value may be an array. |
Custom merge tags
swiftaction()->merge_tags()->register_group( 'ticket', __( 'Ticket', 'myplugin' ),
function ( string $key, \SwiftAction\Engine\RunContext $c ) {
$ticket = myplugin_get_ticket( (int) $c->get( 'ticket_id' ) );
return match ( $key ) {
'subject' => $ticket?->subject,
'url' => $ticket ? get_permalink( $ticket->post_id ) : null,
default => null, // unknown → fallback text
};
},
array( 'subject' => 'Subject', 'url' => 'Link' ) // entries in the picker
);Return new \SwiftAction\Engine\HtmlValue( $html ) to output HTML. For one-off values use the swiftaction_merge_tag_value filter.
Integrations
add_action( 'swiftaction_register_integrations', function () {
swiftaction_register_integration( 'myplugin', array(
'label' => 'My Plugin',
'is_active' => fn() => class_exists( 'MyPlugin' ),
'load' => function ( \SwiftAction\Plugin $plugin ) {
swiftaction_register_trigger( 'myplugin.ticket_closed', array( 'integration' => 'myplugin', /* ... */ ) );
},
) );
} );Contacts, tags and fields
$contact = swiftaction_get_user_contact( $user_id );
$contact = swiftaction()->contacts()->identify( $email, array( 'first_name' => 'Ann' ) ); // guest by email
$contact = swiftaction()->contacts()->current(); // logged-in user or tracked guest
swiftaction_add_tag( $contact, 'VIP' ); // creates the tag if needed; fires "Tag added"
swiftaction_remove_tag( $contact, 'VIP' );
swiftaction_has_tag( $contact, 'VIP' );
swiftaction()->contacts()->meta()->set( $contact->id, 'company', 'Acme' );
swiftaction()->subscriptions()->set( $contact, 'unsubscribed', array( 'source' => 'api' ) );Useful hooks
| Hook | Type | Use |
|---|---|---|
swiftaction_run_started / _finished | action | React to runs (Run, Workflow, Contact). |
swiftaction_step_recorded | action | Every step, including simulations (run ID 0). |
swiftaction_tag_added / _removed | action | Contact, Tag. |
swiftaction_popup_event | action | shown, clicked, dismissed, submitted for popups and banners. |
swiftaction_form_submitted | action | Every stored form submission. |
swiftaction_email_opened / _clicked | action | Tracked opens and clicks. |
swiftaction_contact_unsubscribed / _resubscribed | action | Subscription changes, with the source. |
swiftaction_can_track_guest | filter | Final say on the guest cookie. |
swiftaction_guest_consent | filter | Connect your cookie banner. |
swiftaction_webhook_args | filter | Change webhook requests (auth, timeouts). |
swiftaction_form_values | filter | Validate or change form answers; return WP_Error to reject. |
swiftaction_email_rendered | filter | Change a designed email after rendering. |
swiftaction_unsubscribe_url | filter | Send unsubscribe links to your own preferences page. |
swiftaction_show_popups | filter | Hide automatic popups and banners on a page. |
The plugin ships a complete hooks reference in docs/DEVELOPERS.md.
REST API
Namespace swiftaction/v1. Admin routes need the swiftaction_manage capability (cookie with X-WP-Nonce, or application passwords).
| Route | Purpose |
|---|---|
GET /definitions | Triggers, actions, condition fields, merge tags and tags. |
GET, POST /workflows · GET, PUT, DELETE /workflows/{id} | Workflows (PUT saves a draft). |
POST /workflows/{id}/publish · /status · /duplicate · /validate | Publish, activate or pause, duplicate, validate. |
GET /workflows/{id}/live · /runs | Live counts and events; runs. |
GET /runs/{id} | A run with its steps, graph and email results. |
POST /workflows/{id}/simulate | Dry run for a contact. |
/popups · /emails · /contact-fields · /tags · /contacts | Popups and banners, emails (preview, test), custom fields, tags, contact search. |
Front-end JavaScript
swiftaction.openPopup( 5 ); swiftaction.closePopup();
swiftaction.showBanner( 7 ); swiftaction.hideBanner( 7 );
document.addEventListener( 'swiftaction:popup:submitted', ( e ) => { /* e.detail.id, e.detail.source */ } );
// swiftaction:popup:shown|clicked|dismissed, swiftaction:banner:shown|clicked|dismissed|submittedswiftaction()->contacts(), ->runs(), ->emails()…) rather than querying tables: the schema can change between versions.