LMS & eLearning

Automating LearnDash Course Completion Workflows

Run several actions when a learner finishes a LearnDash course, such as a certificate email, next-course enrollment, a group change and a CRM update, from one queued handler that runs once.

Automating LearnDash Course Completion Workflows

Finishing a course is the moment most sites want several things to happen: send the certificate, enroll the learner in the next course, move them to an alumni group, tell the CRM, perhaps notify a manager. Each of those has a simple snippet on its own. Bolted together as five separate hooks, they become hard to reason about, slow down the request in which the learner clicks Mark Complete, and sometimes run twice. This tutorial builds one completion workflow for LearnDash 4.18.0.1 that runs each step once, off the learner's request, with a record of what happened.

The trigger

LearnDash fires learndash_course_completed once a learner's progress reaches completion. It passes one array with user (a WP_User), course (a WP_Post), progress and course_completed, the completion timestamp. LearnDash itself hooks it at priority 30 to update group progress. The hook and its siblings for lessons, topics and quizzes are described in the completion hooks guide.

The hook runs during the request that completed the last step, often an AJAX mark complete or a quiz submission. Anything slow inside it, such as an HTTP call to a CRM or generating a PDF, delays the learner's next page. That is the main reason to queue the work.

Step 1: record and queue

<?php
/**
 * On completion, record it and queue the workflow.
 *
 * @param array $data Completion data from LearnDash.
 */
function wwc_on_course_completed( $data ) {
    if ( empty( $data['user'] ) || empty( $data['course'] ) ) {
        return;
    }

    $user_id   = (int) $data['user']->ID;
    $course_id = (int) $data['course']->ID;
    $flag      = 'wwc_completion_workflow_' . $course_id;

    // Run once per user and course, even if completion fires again.
    if ( get_user_meta( $user_id, $flag, true ) ) {
        return;
    }
    update_user_meta( $user_id, $flag, time() );

    $args = array(
        'user_id'   => $user_id,
        'course_id' => $course_id,
    );

    if ( function_exists( 'as_enqueue_async_action' ) ) {
        as_enqueue_async_action( 'wwc_run_completion_workflow', array( $args ), 'wwc' );
    } else {
        wp_schedule_single_event( time(), 'wwc_run_completion_workflow', array( $args ) );
    }
}
add_action( 'learndash_course_completed', 'wwc_on_course_completed', 40 );

Completion can fire more than once for the same learner and course, for example after an administrator resets progress and the learner finishes again, or when a step is marked complete twice by a slow double click. The user meta flag makes the workflow run once. Delete the flag on purpose when you reset a learner and want the workflow to run again; the reset tools are covered in resetting course progress.

LearnDash bundles Action Scheduler, so as_enqueue_async_action() is normally available. It runs the job in the background within a minute, retries failures and keeps a log under Tools, Scheduled Actions. The WP-Cron fallback works when Action Scheduler is absent.

Step 2: the workflow

<?php
/**
 * Run each completion step, logging the outcome.
 *
 * @param array $args user_id and course_id.
 */
function wwc_run_completion_workflow( $args ) {
    $user_id   = absint( $args['user_id'] ?? 0 );
    $course_id = absint( $args['course_id'] ?? 0 );
    $user      = get_userdata( $user_id );

    if ( ! $user || ! $course_id ) {
        return;
    }

    $map = array(
        // course_id => settings for what happens after it.
        101 => array( 'next_course' => 102, 'add_group' => 0 ),
        102 => array( 'next_course' => 0, 'add_group' => 350 ),
    );
    $rules = $map[ $course_id ] ?? array();
    $log   = array();

    // 1. Certificate email.
    $certificate = learndash_get_course_certificate_link( $course_id, $user_id );
    if ( $certificate ) {
        $sent  = wp_mail(
            $user->user_email,
            sprintf( __( 'Your certificate for %s', 'wwc' ), get_the_title( $course_id ) ),
            sprintf( __( "Congratulations on completing %1\$s.\n\nDownload your certificate: %2\$s", 'wwc' ), get_the_title( $course_id ), $certificate )
        );
        $log[] = 'certificate email ' . ( $sent ? 'sent' : 'failed' );
    }

    // 2. Next course.
    if ( ! empty( $rules['next_course'] ) && ! sfwd_lms_has_access( $rules['next_course'], $user_id ) ) {
        $log[] = 'enroll ' . $rules['next_course'] . ': ' . ( ld_update_course_access( $user_id, $rules['next_course'] ) ? 'ok' : 'no change' );
    }

    // 3. Group membership.
    if ( ! empty( $rules['add_group'] ) ) {
        $log[] = 'group ' . $rules['add_group'] . ': ' . ( ld_update_group_access( $user_id, $rules['add_group'] ) ? 'ok' : 'no change' );
    }

    // 4. Tell other code, such as a CRM integration.
    do_action( 'wwc_course_workflow_done', $user_id, $course_id, $log );

    update_user_meta( $user_id, 'wwc_completion_log_' . $course_id, $log );
}
add_action( 'wwc_run_completion_workflow', 'wwc_run_completion_workflow' );

The map keeps the business rules in one readable place. On a site with many courses, move it to a course meta field so administrators can set the next course and group per course without code.

What each step relies on

  • Certificate link. learndash_get_course_certificate_link( $course_id, $user_id ) returns the certificate URL when the course has a certificate and the user has completed it, and an empty string otherwise, so the email only goes out when there is something to send. Because PDFs are generated on request, the link works whenever the learner opens it. Certificate setup is covered in attaching a certificate to a course.
  • Enrollment. ld_update_course_access( $user_id, $course_id ) returns true when it made a change and fires learndash_update_course_access, which notifications and integrations listen to. The access check before it avoids pointless writes. Chaining courses this way is explained further in enrolling users when they complete another course.
  • Groups. ld_update_group_access( $user_id, $group_id ) adds the user to the group and returns a boolean, firing ld_added_group_access.
  • Integrations. Firing your own action at the end lets a CRM, Slack or analytics integration subscribe to finished workflows instead of to raw completion, which means it never sees duplicates.

Emails: core, Notifications add-on or your own

LearnDash core sends no completion email. The LearnDash Notifications add-on has a course completed trigger with placeholders, including a certificate link, and is the better choice when non-developers need to edit wording. Use the code above when the email depends on logic the add-on cannot express, or when it must be part of a larger workflow; do not use both for the same message. Our Notifications setup guide covers the add-on.

Redirecting the learner

The workflow runs in the background, so it cannot redirect the learner. To send them to a thank-you page with next steps, use the completion redirect covered in redirecting learners after course completion. The thank-you page can read the completion log from user meta and tell the learner they have been enrolled in the next course, which saves support emails.

Handling failures

A workflow step can fail: the mail server rejects a message, the CRM times out. Action Scheduler marks the action failed when your callback throws an exception, and shows the error in its log, so throw one for failures you want visible and retry by re-running the action from the Scheduled Actions screen. Make each step safe to repeat before you rely on retries: the enrollment and group steps already check state first, and an email step can store a "sent" flag next to the log so a retry does not send it twice.

Testing

Use a short test course mapped to a next course and a group. Complete it as a test learner, then open Tools, Scheduled Actions, filter by the wwc group and confirm the action ran. Check the email arrived with a working certificate link, the next course appears in the learner's profile and the group shows the member. Complete the course again after resetting progress without deleting the flag and confirm nothing runs twice. Finally, break the email deliberately, for example with a mail plugin in test mode, and confirm the log records the failure.

When to get help

A workflow like this is an afternoon's work. Multi-step journeys with waits, conditional branches, manager approvals or several external systems are a larger integration project; our LearnDash services and API integration teams build them, and a LearnDash developer can add an admin screen to edit the rules.

Frequently asked questions

Which hook runs when a LearnDash course is completed?

learndash_course_completed, with an array containing user, course, progress and course_completed.

Why queue the work instead of running it in the hook?

The hook runs in the learner's request. Emails and external calls there slow the next page and can fail without a retry. Action Scheduler runs them in the background with retries and a log.

How do I stop the workflow running twice?

Store a per-course flag in user meta when it first runs and return early when the flag exists.

Does LearnDash send a completion email by itself?

No. Use the Notifications add-on or your own email step as shown above.

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.

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.