LMS & eLearning

Building a LearnDash Add-On: Structure, Dependency Check and Activation

A skeleton for your own LearnDash add-on modelled on the official ones: file layout, a dependency and version check, booting on learndash_init, templates themes can override, upgrades and a clean uninstall.

Building a LearnDash Add-On: Structure, Dependency Check and Activation

Once a LearnDash site collects more than a few snippets, they belong in a plugin of their own. A proper add-on survives theme changes, can be switched off for debugging, and can be versioned and deployed like any other code. This guide builds a skeleton add-on following the patterns the official LearnDash add-ons use in their current releases, checked against LearnDash 4.18.0.1 and the Certificate Builder 1.1.2 source, and explains why each piece is there.

A file layout that scales

wwc-learndash-tools/
    wwc-learndash-tools.php      Main file: header, constants, bootstrap.
    includes/
        class-plugin.php         Wires hooks once LearnDash is ready.
        class-settings-section.php
        class-upgrader.php
    templates/                   Front-end markup themes can override.
    languages/
    uninstall.php

The official add-ons are laid out the same way: a main file that defines constants, loads the text domain and runs a dependency check, then code that only runs once LearnDash has loaded. Keep the main file small, so that a fatal error in a feature file cannot stop the dependency check from running.

The main file

<?php
/**
 * Plugin Name: WWC LearnDash Tools
 * Description: Site-specific LearnDash features.
 * Version:     1.0.0
 * Requires PHP: 7.4
 * Text Domain: wwc-learndash-tools
 */

defined( 'ABSPATH' ) || exit;

define( 'WWC_LDT_VERSION', '1.0.0' );
define( 'WWC_LDT_FILE', __FILE__ );
define( 'WWC_LDT_DIR', plugin_dir_path( __FILE__ ) );
define( 'WWC_LDT_MIN_LEARNDASH', '4.6.0' );

add_action(
    'plugins_loaded',
    function () {
        load_plugin_textdomain( 'wwc-learndash-tools', false, dirname( plugin_basename( WWC_LDT_FILE ) ) . '/languages' );

        if ( ! defined( 'LEARNDASH_VERSION' ) || version_compare( LEARNDASH_VERSION, WWC_LDT_MIN_LEARNDASH, '<' ) ) {
            add_action( 'admin_notices', 'wwc_ldt_dependency_notice' );
            return;
        }

        require_once WWC_LDT_DIR . 'includes/class-plugin.php';
        add_action( 'learndash_init', array( 'WWC_LDT_Plugin', 'boot' ) );
    },
    20
);

/**
 * Explain why the add-on is idle.
 */
function wwc_ldt_dependency_notice() {
    if ( ! current_user_can( 'activate_plugins' ) ) {
        return;
    }

    printf(
        '<div class="notice notice-error"><p>%s</p></div>',
        esc_html(
            sprintf(
                /* translators: %s: minimum LearnDash version. */
                __( 'WWC LearnDash Tools needs LearnDash LMS %s or later to be active.', 'wwc-learndash-tools' ),
                WWC_LDT_MIN_LEARNDASH
            )
        )
    );
}

Why the check looks like that

LEARNDASH_VERSION is defined at the top of the LearnDash main file, so it exists as soon as WordPress has loaded active plugins, whatever order they load in. The rest of LearnDash, including the SFWD_LMS class and the function library, is required inside a plugins_loaded callback at priority 0, which then fires learndash_files_included. Running the check at priority 20 means both are in place, and comparing versions catches a site running an older release that lacks functions you use.

The official add-ons do the same job with a small dependency checker class. Certificate Builder declares that it needs sfwd-lms/sfwd_lms.php, with the class SFWD_LMS, the version constant LEARNDASH_VERSION and a minimum version of 4.6.0. On plugins_loaded at priority 11 the checker tests is_plugin_active(), including network activation, falls back to checking the class, compares versions, and queues an admin notice if anything fails. The add-on then boots on learndash_init only when the results pass. A shorter check like the one above is enough for a site-specific add-on, but the logic is the same: never call LearnDash code until you know it is there.

Booting on learndash_init

LearnDash fires learndash_init during init at priority 1, before it registers settings sections and pages. It is the right moment to add hooks, because post types and settings classes exist, and it is early enough to register your own settings. The order of the LearnDash boot sequence is set out in our guide to LearnDash lifecycle hooks.

<?php
class WWC_LDT_Plugin {

    /**
     * Wire every feature. Runs on learndash_init.
     */
    public static function boot() {
        require_once WWC_LDT_DIR . 'includes/class-upgrader.php';
        WWC_LDT_Upgrader::maybe_upgrade();

        add_action( 'learndash_course_completed', array( __CLASS__, 'on_course_completed' ) );
        add_action( 'learndash_settings_sections_init', array( __CLASS__, 'register_settings' ) );
    }

    /**
     * Example feature: record the completion time of the last course.
     *
     * @param array $data Completion data from LearnDash.
     */
    public static function on_course_completed( $data ) {
        if ( empty( $data['user'] ) || ! $data['user'] instanceof WP_User ) {
            return;
        }

        update_user_meta( $data['user']->ID, 'wwc_last_course_completed_at', time() );
    }

    /**
     * Register the add-on's settings section.
     */
    public static function register_settings() {
        require_once WWC_LDT_DIR . 'includes/class-settings-section.php';
        WWC_LDT_Settings_Section::add_section_instance();
    }
}

The settings class extends LearnDash_Settings_Section, so your options appear inside LearnDash's own settings screens with its field types and saving. Building that class is covered step by step in our guide to adding a custom LearnDash settings section.

Templates themes can override

If the add-on prints front-end markup, put it in template files and let themes override them. LearnDash's own lookup goes through SFWD_LMS::get_template_paths(), which searches the theme for learndash/{active template set}/{file}. Official add-ons use their own folder under learndash/ instead; Course Grid, for example, calls locate_template( 'learndash/course-grid/' . $file ) and falls back to its bundled copy. Do the same with your add-on's name:

<?php
/**
 * Render an add-on template, preferring a theme copy.
 *
 * @param string $file Template file relative to templates/.
 * @param array  $args Variables for the template.
 */
function wwc_ldt_template( $file, array $args = array() ) {
    $template = locate_template( 'learndash/wwc-learndash-tools/' . $file );

    if ( '' === $template ) {
        $template = WWC_LDT_DIR . 'templates/' . $file;
    }

    if ( is_readable( $template ) ) {
        load_template( $template, false, $args );
    }
}

Templates receive the array as $args. Escape everything inside them, because a theme developer copying the file will copy your escaping habits too.

Loading assets only where they are needed

A common performance mistake in custom add-ons is enqueueing scripts and styles on every page. Check the context first. On the front end, learndash_get_post_type_slug() returns the real post type names for courses, lessons, topics and quizzes, so compare get_post_type() against those before enqueueing, and make your stylesheet depend on learndash-front so it loads after the ld30 styles. In the admin, use the screen ID passed to admin_enqueue_scripts. The same discipline applies to AJAX: register handlers only for logged-in users unless visitors genuinely need them, and verify a nonce and a capability in each one. Our guide to nonces, capabilities and sanitization in LearnDash code covers those checks with examples.

Upgrades between versions

Store the add-on's data version in an option and compare it on boot, so database changes run once after each update rather than only on activation, which does not fire when a plugin is updated in place:

<?php
class WWC_LDT_Upgrader {

    const OPTION = 'wwc_ldt_db_version';

    /**
     * Run pending upgrade steps once.
     */
    public static function maybe_upgrade() {
        $installed = get_option( self::OPTION, '0' );

        if ( version_compare( $installed, WWC_LDT_VERSION, '>=' ) ) {
            return;
        }

        if ( version_compare( $installed, '1.0.0', '<' ) ) {
            add_option( 'wwc_ldt_settings', array( 'enabled' => 'yes' ) );
        }

        update_option( self::OPTION, WWC_LDT_VERSION );
    }
}

Long-running upgrades over many users belong in background batches. LearnDash bundles Action Scheduler, which you can use once LearnDash has loaded.

Uninstall

An uninstall.php file runs when the plugin is deleted from the Plugins screen. Check for WP_UNINSTALL_PLUGIN, then delete only data your add-on created, such as its options and user meta keys. Never delete LearnDash data from an add-on's uninstall, even data your add-on wrote into LearnDash tables, because the site owner deleting your plugin has not asked to delete course progress.

Checklist before deploying

  • Deactivate LearnDash on staging and confirm the add-on shows its notice instead of a fatal error.
  • Run with WP_DEBUG on and check for notices from your hooks.
  • Prefix every function, class, option and hook name.
  • Escape output, sanitize input, and check nonces and capabilities on every form and AJAX handler.
  • Test a theme override of one template.

Where snippets should live before they grow into an add-on is weighed in our guide to LearnDash's plugin architecture, which also explains the service container the newer add-ons register with.

When to get help

Turning a folder of snippets into a maintained add-on is a common first engagement for our LearnDash development team, and our LearnDash services page lists the integrations we build on this skeleton. A LearnDash developer can also add automated tests and a release process.

Frequently asked questions

When is it safe to call LearnDash functions from an add-on?

After LearnDash's files are included on plugins_loaded at priority 0. Hooking learndash_init or learndash_files_included guarantees it.

How do the official add-ons check for LearnDash?

They use a dependency checker that tests whether the plugin is active, falls back to the SFWD_LMS class, compares LEARNDASH_VERSION with a minimum and shows an admin notice when the check fails.

Why not run upgrades on activation?

Activation hooks do not fire when a plugin is updated in place. Comparing a stored version on boot catches every update.

Where should theme overrides of add-on templates go?

Use a folder under the theme's learndash directory named after your add-on, as Course Grid does with learndash/course-grid/.

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.