LogoLogo
WordPress.orgExtensionsSupport
Version 6
Version 6
  • Notification – Custom Notifications and Alerts for WordPress
  • Known issues
  • User guide
    • How Notification plugin works
    • Who can use this plugin
    • How to create Notifications
    • Custom Post Type support
    • How to escape { character
    • Background processing
    • Troubleshooting
    • How to setup different FROM address for different Notifications
  • Developer
    • General
      • Plugin loading chain
      • Extension possibilities
      • Creating an extension
      • Bundling Notification plugin
      • White label mode
    • Notifications
      • Suppressing the Notification
      • JSON synchronization
      • Programmatic Notifications
    • Triggers
      • List of all default Triggers
      • Custom Trigger
      • Enable support for non-public Custom Post Type
      • Adding Merge Tags to existing Triggers
      • Postponing the Trigger action
      • Delaying Trigger execution with Cron
    • Carriers
      • Suppressing the Carrier
    • Snippets
      • General
        • Automatic Trigger testing
        • Allow other roles to edit Notifications
      • Triggers
        • Post
        • User
      • Integations
        • WP All Import
        • MemberPress
        • Gutenberg
  • Extensions
    • Installation
    • Planned extensions
    • 3rd Party Extensions
    • Custom Fields
    • Scheduled Triggers
Powered by GitBook
On this page
  • Basic example
  • All arguments

Was this helpful?

  1. Developer
  2. Notifications

Programmatic Notifications

PreviousJSON synchronizationNextTriggers

Last updated 5 years ago

Was this helpful?

The notifications doesn't necessarly have to be saved in WordPress database nor loaded from JSON files. A good example is where all the notifications are created on the fly.

Basic example

You can define the notification with a simple array. Below is a minimal example with all the required parameters.

if ( function_exists( 'notification' ) ) :
notification( [
	'title'    => 'My programmatic notification', // For internal reference.
	'trigger'  => 'trigger_slug', // Trigger slug (can be a Triggerable object).
	'carriers' => [ // An array with format: carrier_slug => data array
		'email' => [
			'activated'  => true, // Must be true.
			'enabled'    => true, // Must be true.
			'subject'    => 'Email from ghost notification!', 
			'body'       => 'This is nice, {user_first_name}, huh?',
			'recipients' => [
				[
					'type'      => 'administrator',
					'recipient' => '',
				],
				[
					'type'      => 'email',
					'recipient' => '{user_email}',
				],
			],
		],
	],
	'enabled'  => true, // Must be true.
] );
endif;

All arguments

All arguments available:

if ( function_exists( 'notification' ) ) :
notification( [
	'hash'     => 'my_programmatic_notification', // Unique notification hash, automatically generated.
	'title'    => 'My programmatic notification',
	'trigger'  => 'trigger_slug',
	'carriers' => [
		'email' => [
			'activated'  => true,
			'enabled'    => true,
			'subject'    => 'Email from ghost notification!', 
			'body'       => 'This is nice, {user_first_name}, huh?',
			'recipients' => [
				[
					'type'      => 'administrator',
					'recipient' => '',
				],
				[
					'type'      => 'email',
					'recipient' => '{user_email}',
				],
			],
		],
	],
	'enabled'  => true,
	'extras'   => [], // Extra data array, ie. config for add-ons.
	'version'  => time(), // Version of the notification, should be a timestamp. Default: current time.
] );
endif;

You can reference the Carrier fields by . You should check the add_form_field method calls which will contain the field slug and other useful info about the type.

dynamic trigger testing
checking the source code