Notification
WordPress.orgExtensionsSupport
Version 7
Version 7
  • Notification – Custom Notifications and Alerts for WordPress
  • Known issues
  • πŸ€Έβ€β™€οΈ User guide
    • How Notification plugin works
    • Who can use this plugin
    • How to create Notifications
    • Troubleshooting
    • Advanced
      • How to escape { character
      • Background processing
      • How to setup different FROM address for different Notifications
      • Custom Post Type support
  • πŸ”§Developer
    • General
      • Plugin loading chain
      • Runtime
      • 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
      • Custom Carrier
      • Adding custom fields to Carrier form
      • Suppressing the Carrier
    • Recipients
      • Custom Recipient
    • Snippets
      • General
        • Automatic Trigger testing
        • Allow other roles to edit Notifications
        • Allow sending Notification while logging is active
        • Programmatic Notification with manual Trigger
        • Background Processing filter
      • Triggers
        • Post
        • User
      • Integations
        • WP All Import
        • MemberPress
        • Gutenberg
  • 🧩Extensions
    • Installation
    • Planned extensions
    • 3rd Party Extensions
    • Custom Fields
      • v1.3
    • Scheduled Triggers
    • Slack
    • Twilio
Powered by GitBook
On this page
  • Trigger
  • Notification
  • Action
  1. Developer
  2. Snippets
  3. General

Programmatic Notification with manual Trigger

Send the notification from the code, nothing needs to be configured in the Dashboard.

To make it work you'll need:

  • Custom Manual Trigger

  • Notification definition

  • Action to trigger the Notification

Trigger

Let's define a simple Trigger without any Merge Tags.

add_action( 'notification/init', function() {

/**
 * Manual trigger class
 */
class ManualTrigger extends \BracketSpace\Notification\Abstracts\Trigger {

	/**
	 * Constructor
	 */
	public function __construct() {

		parent::__construct(
			'support/manual_trigger',
			'Manual Trigger'
		);


		$this->add_action( 'notification_manual_trigger_348m7t5', 10 );

		$this->set_group( 'Support' );

		$this->set_description( 'Triggered with notification_manual_trigger_348m7t5 action.' );

	}

	/**
	 * Action callback
	 *
	 * @return void
	 */
	public function action() {
		// This trigger should always process.
	}

	/**
	 * Merge Tags
	 *
	 * @return void
	 */
	public function merge_tags() {
		// This trigger doesn't include any Merge Tags.
	}

}

} );

Notification

The notification is just a function call with a configuration array. We'll register the Notification along with the Trigger.

add_action( 'notification/elements', function() {
	$manual_trigger = new ManualTrigger;

	notification_register_trigger( $manual_trigger );

	notification( [
		'hash'     => 'notification_manual_trigger_348m7t5',
		'title'    => 'Notification manual trigger',
		'trigger'  => $manual_trigger,
		'carriers' => [
			'email' => [
				'activated'  => true,
				'enabled'    => true,
				'subject'    => 'Manual trigger test',
				'body'       => 'Hello from the other side!',
				'recipients' => [
					[
						'type'      => 'administrator',
						'recipient' => '',
					],
				],
			],
		],
		'enabled'  => true,
		'extras'   => [],
		'version'  => time(),
	] );
} );

In this example, we using a simple email Carrier, but you are free to use any other Carrier registered within the Notification plugin. The easiest way to get the Carrier configuration is to set it up in WordPress Dashboard and just export the Notification to JSON. The JSON file will have all the keys you need to configure here.

Action

Our Trigger uses notification_manual_trigger_348m7t5 action hook, so the only one thing left is to call it somewhere. The simplest would be:

do_action( 'notification_manual_trigger_348m7t5' );

It can be convenient to wrap this with a simple $_GET param check.

add_action( 'init', function() {
	if ( isset( $_GET['i3m84ty'] ) ) {
		do_action( 'notification_manual_trigger_348m7t5' );
	}
} );

And call it like this: example.com?i3m84ty

PreviousAllow sending Notification while logging is activeNextBackground Processing filter

Last updated 4 years ago

You need to wrap the class definition with notification/init action, because .

Read more about

Read more about

πŸ”§
this is the earliest the abstract classes are accessible
Custom Triggers
Programmatic Notifications