Registering new triggers

You can register own trigger anywhere in your code.

Only one thing to remember is that you have to do it later than on the init action with priority 5.

$args = array(
    'slug'       => (string) (required) trigger slug,
    'name'       => (string) (required) trigger human readable translated name,
    'group'      => (string) (optional) trigger group name in the triggers select (translated),
    'title'      => (string) (optional) default email subject which will be populated after selecting the trigger,
    'template'   => (string) (optional) default template which will be populated to email content after selecting the trigger,
    'recipients' => (array) (optional) default email recipients which will be populated after selecting the trigger,
    'disable'    => (array) (optional) array with values: ('post', 'comment', 'user'), determines if trigger can be disabled on the object edit page,
    'tags'       => (array) (optional) merge tags
);
register_trigger( $args );

tags array must contain tags in following format:

array(
    'tag slug' => 'tag type',
    'tag slug' => 'tag type',
)

And you can use following types:

  • integer
  • float
  • string
  • url
  • email
  • boolean
  • ip

You must pay attention to provide the exact type you are going to use.

recipients array format is:

array(
    'recipient_type_slug' => 'recipient value',
    'email'               => 'my@email.com',
    'administrator'       => '',
    'role'                => 'contributor',
    'merge_tag'           => 'merge_tag_slug',
)

Examples

function add_notification_trigger() {

	register_trigger( array(
	    'slug'     => 'my_plugin/action',
	    'name'     => __( 'Custom action', 'textdomain' ),
	    'group'    => __( 'My Plugin', 'textdomain' ),
	    'template' => 'This is default template using {merge_tag}. It can accept <strong>HTML</strong>',
	    'tags'     => array(
	        'page_ID'    => 'integer',
	        'page_url'   => 'url',
	        'user_email' => 'email'
	    )
	) );
	
}
add_action( 'init', 'add_notification_trigger' );

You can also provide more advanced default template by calling the function.

function notification_template() {

	$html = '<p>' . __( 'Howdy!', 'textdomain' ) . '</p>';

	/* translators: please do not translate strings in { } */
	$html .= '<p>' . __( 'Check my cool page: {page_url}!', 'textdomain' ) . '</p>';

	return $html;

}

function add_notification_trigger() {

	register_trigger( array(
	    'slug'     => 'my_plugin/action',
	    'name'     => __( 'Custom action', 'textdomain' ),
	    'group'    => __( 'My Plugin', 'textdomain' ),
	    'template' => call_user_func( 'notification_template' ),
	    'tags'     => array(
	        'page_ID'    => 'integer',
	        'page_url'   => 'url',
	        'user_email' => 'email'
	    )
	) );
	
}
add_action( 'init', 'add_notification_trigger' );

Minimal setup:

function add_notification_trigger() {

	register_trigger( array(
	    'slug'     => 'my_plugin/action',
	    'name'     => __( 'Custom action', 'textdomain' ),
	) );
	
}
add_action( 'init', 'add_notification_trigger' );