LMS & eLearning

Building a Custom Trigger Class for LearnDash Notifications

How the LearnDash Notifications add-on 1.6.4 structures its triggers, the abstract Trigger class contract (listen, monitor, send, queue), how built-in triggers are registered, and a custom trigger for group completion.

Building a Custom Trigger Class for LearnDash Notifications

The LearnDash Notifications add-on ships seventeen triggers, from course enrollment to assignment approval. Each is a small class that extends one abstract base, listens to a LearnDash action, finds the notifications configured for its trigger key, checks their conditions and either sends or queues the emails. The base class does all the heavy lifting, so a new trigger for an event the add-on does not cover is mostly a matter of choosing the action to listen to and passing the right arguments. This guide explains the contract in version 1.6.4 and builds a trigger for a learner completing every course in a group.

How triggers are wired

On load the add-on instantiates a fixed list of trigger classes in its namespace and calls listen() on each. The list is hard-coded in the plugin's bootstrap; there is no filter for adding classes to it. A custom trigger is therefore instantiated by your own plugin, after the add-on has loaded, and you call listen() yourself. The admin side is separate: the trigger dropdown on the notification edit screen comes from learndash_notifications_get_triggers(), which returns a key-to-label array through the learndash_notifications_triggers filter. Adding your key there lets administrators create notifications for it.

The abstract Trigger class

Your class extends LearnDash_Notification\Trigger and sets a protected $trigger property to the key. The base provides:

  • get_notifications( $type ): published notification posts whose _ld_notifications_trigger meta equals the key, as Notification models with their recipients, delay and conditions.
  • is_valid( $model, $args ): runs the condition checks (course, lesson, topic, quiz, assignment and group conditions, evaluated against the user and object IDs in the arguments) and the triggering-object checks, each filterable through learndash_notifications_are_conditions_valid and learndash_notifications_are_triggering_objects_valid, and the overall learndash_notifications_trigger_valid.
  • send( $emails, $model, $args ): builds the subject and content through the add-on's shortcode parsing and the email filters, sends to each address, and fires learndash_notifications_email_sent or learndash_notifications_email_failed per recipient.
  • queue_use_db( $emails, $model, $args ): inserts a row into the delayed emails table with the send time computed from the model's delay and unit.
  • send_db_delayed_email(): processes the next due row; built-in triggers hook it to the add-on's delayed-email action.
  • log( $message ): writes to the add-on's log directory under uploads when logging is enabled.

The one abstract method is listen(). By convention each trigger also has a public monitor() callback that receives the LearnDash action's arguments and a protected can_send_delayed_email( $model, $args ) that re-validates when a queued email comes due.

How a built-in trigger looks

The lesson-completion trigger hooks learndash_lesson_completed. Its monitor extracts the course, user and lesson from the arguments, fetches notifications for complete_lesson, and for each one that passes is_valid() with the user, course and lesson IDs, gathers the recipient emails from the model and either queues (if the model has a delay) or sends. That is the whole pattern; our overview of LearnDash completion hooks lists the actions you can listen to.

A custom trigger: group fully completed

LearnDash fires learndash_course_completed when a learner finishes a course. A trigger that fires when the learner has completed every course in one of their groups is not built in. Here it is:

<?php
namespace WWC\Notifications;

use LearnDash_Notification\Notification;
use LearnDash_Notification\Trigger;

class Group_Completed extends Trigger {
	protected $trigger = 'wwc_group_completed';

	public function listen() {
		add_action( 'learndash_course_completed', [ $this, 'monitor' ] );
		add_action( 'leanrdash_notifications_send_delayed_email', [ $this, 'send_db_delayed_email' ] );
	}

	public function monitor( $data ) {
		$user   = $data['user'] ?? null;
		$course = $data['course'] ?? null;

		if ( ! is_object( $user ) || ! $course instanceof \WP_Post ) {
			return;
		}

		$models = $this->get_notifications( $this->trigger );
		if ( empty( $models ) ) {
			return;
		}

		foreach ( learndash_get_users_group_ids( $user->ID ) as $group_id ) {
			$course_ids = learndash_group_enrolled_courses( $group_id );
			if ( ! in_array( $course->ID, array_map( 'intval', $course_ids ), true ) ) {
				continue;
			}

			foreach ( $course_ids as $course_id ) {
				if ( ! learndash_course_completed( $user->ID, $course_id ) ) {
					continue 2;
				}
			}

			$args = [ 'user_id' => $user->ID, 'course_id' => $course->ID, 'group_id' => $group_id ];

			foreach ( $models as $model ) {
				if ( ! $this->is_valid( $model, $args ) ) {
					continue;
				}

				$emails = $model->gather_emails( $user->ID, $course->ID );

				if ( absint( $model->delay ) ) {
					$this->queue_use_db( $emails, $model, $args );
				} else {
					$this->send( $emails, $model, $args );
				}
			}
		}
	}

	protected function can_send_delayed_email( Notification $model, $args ) {
		return $this->is_valid( $model, $args );
	}
}

add_action( 'plugins_loaded', function () {
	if ( class_exists( 'LearnDash_Notification\Trigger' ) ) {
		( new Group_Completed() )->listen();
	}
}, 20 );

add_filter( 'learndash_notifications_triggers', function ( array $triggers ) {
	$triggers['wwc_group_completed'] = 'User completes every course in a group';
	return $triggers;
} );

The delayed-email action name contains the add-on's own misspelling, leanrdash_notifications_send_delayed_email; copy it exactly or queued emails for your trigger never go out.

Conditions and recipients in the admin

When an administrator creates a notification with your trigger, the edit screen shows the recipient fields (user, group leaders, admins, extra addresses) and the condition fields the add-on's admin script knows about. The condition checks in is_valid() are keyed by condition type, so course and group conditions evaluate correctly as long as your arguments include user_id and course_id. Shortcodes in the email body resolve from those same arguments: user and course shortcodes work, lesson and quiz ones do not for this trigger. Keep the arguments array consistent between send and queue so delayed emails resolve the same values.

Choosing the right action to listen to

Pick the LearnDash action that fires once, with the user and object available, at the moment you want to notify. Completion actions pass an array with user, course and step objects; enrollment fires learndash_update_course_access with IDs and a remove flag, so check the flag before sending; quiz actions pass quiz data arrays keyed by attempt. Avoid actions that fire on every page load or on save in the admin, and avoid hooking save_post for anything learner-facing. If no LearnDash action matches, a scheduled check through WP-Cron that calls your monitor with the same arguments array is the pattern the add-on itself uses for its expiry and inactivity triggers.

Testing

  1. Enable the add-on's logging, create a notification with your trigger and a test recipient.
  2. Complete the last course of a group as a test learner and confirm the email and the log entries.
  3. Set a delay, repeat, and check the delayed emails table row, then run the add-on's cron to send it.
  4. Confirm the mail path works at all; our note on WordPress not sending emails covers delivery.

Because the trigger list is hard-coded, an add-on update does not disturb your class, but changes to the base class signatures could; pin the version you tested against and re-test on updates. Triggers that need their own condition fields in the admin, or that fire from non-LearnDash events such as a CRM webhook, are custom plugin work our LearnDash development team does regularly; see our LearnDash services or talk to a LearnDash developer about the event you want to notify on.

Frequently asked questions

Is there a filter to register a custom trigger class in LearnDash Notifications?

No. The class list is hard-coded. Instantiate your class after the add-on loads and call its listen() method; add the trigger key to the admin dropdown with the learndash_notifications_triggers filter.

Can a custom trigger use delays?

Yes. Call queue_use_db() when the notification has a delay and hook send_db_delayed_email() to the add-on's delayed-email action, implementing can_send_delayed_email() to re-check validity.

Which conditions work with a custom trigger?

Those whose checks the base class can evaluate from your arguments: course, group, lesson, topic, quiz and assignment conditions need the matching IDs in the arguments array.

Will my trigger survive add-on updates?

It is not touched by updates because it lives in your plugin, but it depends on the base class API. Re-test after each Notifications add-on update.

Add WorldWin Coder to your preferred sources

Add worldwincoder.com as a preferred source on Google, or open this article in your AI assistant to use it as a source.

· Developers and project managers

Articles written and reviewed by the developers and project managers at WorldWin Coder, who build and maintain websites, web applications and mobile apps on WordPress, Moodle, PHP, Laravel, CodeIgniter and Flutter.

Free consultation

Need help with LearnDash?

Tell us what you are working on. A developer who does this every week reads your message and replies within one business day with next steps and a free quote.

  • Free consultation and quote
  • NDA on request
  • You own the source code
  • Reply within one business day

Tell us about your project

Add budget and timeline optional, helps us quote faster

This form is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

Keep reading

Have something to build, fix or move?

Tell us where you are. A developer reads every inquiry, and you get next steps and a free quote within one business day.