LMS & eLearning

Adding a Custom Block to the LearnDash Blocks Category

How LearnDash registers its blocks on top of shortcodes, how the LearnDash LMS Blocks category is added, and how to build your own server-rendered block that appears alongside them, with PHP and editor script.

Adding a Custom Block to the LearnDash Blocks Category

Editors expect LearnDash features to appear in the block inserter under LearnDash LMS Blocks, not in a separate category of your own. If you have written a custom shortcode, turning it into a block that sits in that category is a small amount of work, and LearnDash 4.18.0.1 gives you a base class that does most of it. This guide explains how LearnDash's own blocks are built, how the category is registered, and then builds a block for a custom shortcode in two files. It assumes you have a shortcode already; our guide to registering a custom LearnDash shortcode builds the one used below.

How LearnDash blocks work

Every LearnDash block is a thin wrapper over a shortcode. The abstract class LearnDash_Gutenberg_Block holds four properties set by each subclass: shortcode_slug, block_slug, block_attributes and self_closing. Its init() method hooks register_blocks() to init, where it calls register_block_type() with the name learndash/{block_slug}, taken from the block_base property, the attributes array and a render_callback of render_block(). A subclass may set block_dir instead, in which case a block.json directory is registered. At render time render_block() preprocesses the attributes, runs them through learndash_block_markers_shortcode_atts with the shortcode and block slugs, builds the shortcode string and executes it, wrapping the output in a learndash-block-inner container through render_block_wrap(). The editor preview uses the same server render, so what editors see is the real output. Our blocks to shortcodes map lists every pairing.

How the category is added

In includes/gutenberg/lib/enqueue-scripts.php a function learndash_block_categories() checks whether a category with slug learndash-blocks exists in the passed array and, if not, merges one in with the title LearnDash LMS Blocks, placed first. It is attached through learndash_block_categories_all() on the block_categories_all filter at priority 30. Any block whose editor registration sets category: 'learndash-blocks' appears there; LearnDash's own index.js files do exactly that. You do not register the category yourself, you only reference it, and because the filter runs late, it exists by the time your block's script loads.

Option one: extend LearnDash_Gutenberg_Block

If your block is a shortcode wrapper, extending the base class means you write no render code. Set block_base to your own prefix so the block is named after your plugin rather than registered under learndash/.

<?php
add_action(
    'plugins_loaded',
    function () {
        if ( ! class_exists( 'LearnDash_Gutenberg_Block' ) ) {
            return;
        }

        class Acme_Block_Next_Step extends LearnDash_Gutenberg_Block {
            public function __construct() {
                $this->block_base       = 'acme';
                $this->shortcode_slug   = 'acme_next_step';
                $this->block_slug       = 'next-step';
                $this->self_closing     = true;
                $this->block_attributes = array(
                    'course_id' => array( 'type' => 'string' ),
                    'label'     => array( 'type' => 'string' ),
                    'className' => array( 'type' => 'string' ),
                );
                $this->init();
            }
        }

        new Acme_Block_Next_Step();
    },
    20
);

Calling init() from the constructor mirrors how LearnDash instantiates its blocks. The className attribute lets the Additional CSS Class field pass through; the base class drops unknown and empty attributes before building the shortcode, so only values the editor set reach your handler.

The editor script

The PHP side registers the block type and its render callback, but the inserter needs a client-side registration with the same name, the category and controls. A minimal script using the WordPress packages and ServerSideRender:

( function ( blocks, element, components, blockEditor, serverSideRender ) {
    var el = element.createElement;

    blocks.registerBlockType( 'acme/next-step', {
        title: 'Next step link',
        icon: 'controls-forward',
        category: 'learndash-blocks',
        attributes: {
            course_id: { type: 'string' },
            label: { type: 'string' }
        },
        edit: function ( props ) {
            return el( element.Fragment, {},
                el( blockEditor.InspectorControls, {},
                    el( components.PanelBody, { title: 'Settings' },
                        el( components.TextControl, {
                            label: 'Course ID (blank for current)',
                            value: props.attributes.course_id || '',
                            onChange: function ( v ) { props.setAttributes( { course_id: v } ); }
                        } ),
                        el( components.TextControl, {
                            label: 'Button label',
                            value: props.attributes.label || '',
                            onChange: function ( v ) { props.setAttributes( { label: v } ); }
                        } )
                    )
                ),
                el( serverSideRender, { block: 'acme/next-step', attributes: props.attributes } )
            );
        },
        save: function () { return null; }
    } );
} )( window.wp.blocks, window.wp.element, window.wp.components, window.wp.blockEditor, window.wp.serverSideRender );

Enqueue it on enqueue_block_editor_assets with dependencies wp-blocks, wp-element, wp-components, wp-block-editor and wp-server-side-render. Returning null from save stores only the block comment with its attributes, which is what a dynamic block needs and what LearnDash's blocks do. In the editor the server render executes your shortcode as the editing user, so an administrator previewing a page sees their own next step; that is normal for LearnDash blocks too.

Option two: a plain dynamic block

If your block is not a shortcode, register it with register_block_type() and your own render_callback, and still set category: 'learndash-blocks' in the script. You lose the attribute filter and the wrapper, but you avoid the shortcode indirection. Use this for blocks that render large data sets or need a different output wrapper. Either way, do the access check inside the render callback with sfwd_lms_has_access(), because the block editor stores attributes, not permissions.

Course step pages and example IDs

LearnDash blocks placed inside a course, lesson or topic resolve the course from context, and in the editor they use an example post ID, filterable through learndash_gutenberg_block_example_id, so that course-aware blocks preview with real data. If your block relies on learndash_get_course_id(), it will behave the same way on the front end; in the editor, fall back to the first course the editing user can access, or accept a course ID attribute as above, so that the preview is not empty.

When to get help

Wrapping a shortcode as a block is a couple of hours. Blocks with live editor controls bound to LearnDash data, such as course pickers that respect the editor's group leader scope, or blocks that must also work inside Elementor and the Course Grid, are development work our LearnDash services team and LearnDash development practice take on; a LearnDash developer can also port a set of theme shortcodes to blocks in one pass.

Frequently asked questions

How do I put my block in the LearnDash LMS Blocks category?

Set category: 'learndash-blocks' in the block's editor registration. LearnDash adds that category on the block_categories_all filter whenever it is missing.

Do I have to extend LearnDash_Gutenberg_Block?

No. It saves work when your block wraps a shortcode, but a plain dynamic block with its own render callback works and can still use the LearnDash category.

Why is my block registered as learndash/my-block?

The base class builds the name from the block_base property, which defaults to learndash. Set it to your own prefix in your constructor.

Why does the editor preview show the administrator's data?

Server-side rendering runs the shortcode as the editing user. Accept a course ID attribute or add an editor-only fallback so previews are meaningful.

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.