LMS & eLearning

Snippet: Add a Custom Tab to LearnDash Course and Lesson Pages

How the ld30 content tabs are built, the learndash_content_tabs filter and its keys, and snippets that add a Resources or Discussion tab, reorder tabs, and show a tab only for enrolled learners.

Snippet: Add a Custom Tab to LearnDash Course and Lesson Pages

Every LearnDash course, lesson, topic and quiz page in the ld30 theme can show two tabs above the body: Content and, when the step has materials, Materials. Many sites want a third, such as Resources, Transcript, Discussion or Instructor, and the usual workaround is to paste a shortcode into the materials field. LearnDash 4.18.0.1 passes the tab array through a filter before rendering, so adding a tab is a few lines, with full control over its label, icon, content and the condition under which it appears. This article documents the filter and gives three snippets.

How the tabs are built

The course, lesson, topic and quiz templates hand the body and materials to themes/ld30/templates/modules/tabs.php, and the group template uses tabs_group.php, which is identical for this purpose. The module fires learndash-content-tabs-before and a context-specific learndash-{context}-content-tabs-before, then builds an array of two tabs and applies apply_filters( 'learndash_content_tabs', $tabs, $context, $course_id, $user_id ). Each tab is an array with:

  • id, used in the tab's CSS classes and to link the tab button to its panel;
  • icon, a class from LearnDash's icon font such as ld-icon-content or ld-icon-materials;
  • label, the visible text;
  • content, the HTML of the panel;
  • optionally condition, a boolean; when the key exists and is false the tab is skipped.

The Materials tab ships with condition set to whether the materials field is non-empty, which is why it disappears on steps without materials. If only one tab survives, the module prints the content without the tab bar at all, so adding a tab to a step without materials turns the bar on.

$context is course, lesson, topic, quiz or group, and in the group template the third argument is the group ID rather than a course ID.

Snippet 1: a Resources tab from a custom field

<?php
add_filter(
    'learndash_content_tabs',
    function ( $tabs, $context, $course_id, $user_id ) {
        if ( ! in_array( $context, array( 'course', 'lesson', 'topic' ), true ) ) {
            return $tabs;
        }

        $resources = get_post_meta( get_the_ID(), 'wwc_resources', true );

        $tabs[] = array(
            'id'        => 'resources',
            'icon'      => 'ld-icon-materials',
            'label'     => __( 'Resources', 'wwc' ),
            'content'   => wp_kses_post( wpautop( do_shortcode( (string) $resources ) ) ),
            'condition' => '' !== trim( (string) $resources ),
        );

        return $tabs;
    },
    10,
    4
);

The content comes from a post meta field on the current step, so editors fill it per lesson. Running do_shortcode() lets them embed a download list or a form; wp_kses_post() keeps the result to post-safe HTML. The condition hides the tab when the field is empty, matching how Materials behaves.

Snippet 2: a Discussion tab for enrolled learners only

Because the filter receives the user ID, a tab can depend on access. This shows the step's comments in a tab for enrolled learners and hides it from visitors:

<?php
add_filter(
    'learndash_content_tabs',
    function ( $tabs, $context, $course_id, $user_id ) {
        if ( 'lesson' !== $context && 'topic' !== $context ) {
            return $tabs;
        }

        $enrolled = ! empty( $user_id ) && sfwd_lms_has_access( $course_id, $user_id );

        ob_start();
        comments_template();
        $discussion = ob_get_clean();

        $tabs[] = array(
            'id'        => 'discussion',
            'icon'      => 'ld-icon-comments',
            'label'     => __( 'Discussion', 'wwc' ),
            'content'   => $discussion,
            'condition' => $enrolled && comments_open(),
        );

        return $tabs;
    },
    10,
    4
);

Comments must be enabled on the lesson post type in LearnDash LMS > Settings > General and on the individual post for comments_open() to be true. If the theme already prints comments below the lesson, remove that output or you will see them twice; most themes respect comments_open() but not all. The Discussion tab's content is rendered on every page load, so on busy discussion threads consider pagination in the Discussion settings.

Snippet 3: reorder or rename tabs

Tabs render in array order. To put your tab first and rename Content to Lesson, rebuild the array:

<?php
add_filter(
    'learndash_content_tabs',
    function ( $tabs, $context, $course_id, $user_id ) {
        $by_id = array();
        foreach ( $tabs as $tab ) {
            $by_id[ $tab['id'] ] = $tab;
        }

        if ( isset( $by_id['content'] ) && 'lesson' === $context ) {
            $by_id['content']['label'] = __( 'Lesson', 'wwc' );
        }

        $order   = array( 'resources', 'content', 'materials', 'discussion' );
        $ordered = array();
        foreach ( $order as $id ) {
            if ( isset( $by_id[ $id ] ) ) {
                $ordered[] = $by_id[ $id ];
                unset( $by_id[ $id ] );
            }
        }

        return array_merge( $ordered, array_values( $by_id ) );
    },
    20,
    4
);

Priority 20 runs after the tabs added at priority 10, so the reorder sees them. Unknown tabs added by other plugins are appended at the end rather than lost.

Icons

The icon value is a class name from LearnDash's own icon font, loaded with the ld30 styles. ld-icon-content, ld-icon-materials, ld-icon-comments, ld-icon-certificate and ld-icon-download are among those defined in the ld30 stylesheet; inspect the tab bar on an existing page to see the markup, or pass an empty string for no icon.

Focus Mode and themes

Focus Mode uses the same tabs module inside its content area, so the tabs appear there too. Note that the Focus Mode template prints the comments section itself whenever comments are open, so the Discussion tab from snippet 2 would duplicate it there; skip that tab when Focus Mode is enabled, or return false from the learndash_focus_mode_show_existing_comments check and close comments on the step so only the tab remains. A theme that overrides modules/tabs.php may have removed the filter; check the override, as described in our template system guide. Keep the snippets in a child theme or mu-plugin per our update-safe customisation guide.

Testing

  1. Add a value to the resources field on one lesson and view it: three tabs, Resources last.
  2. View a lesson with no resources and no materials: no tab bar, body only.
  3. Log out and view an open course's lesson: Discussion absent; log in as an enrolled learner: present.
  4. Switch tabs with the keyboard; LearnDash's tab script handles focus for added tabs the same as built-in ones.

When to get help

One tab from a custom field is a short task. Tabs that pull from an external system, per-course tab sets managed in the admin, or a Discussion tab backed by a forum plugin are small projects for our LearnDash services team, and a LearnDash developer can also add the editor meta box so authors manage tab content themselves.

Frequently asked questions

Which filter adds tabs to LearnDash lesson pages?

learndash_content_tabs, applied in the ld30 tabs module with the tab array, the context, the course or group ID and the user ID.

Why is the Materials tab missing on some lessons?

Its condition key is false when the materials field is empty. Your own tabs can use the same key to appear only when they have content.

Can a tab show only to enrolled learners?

Yes. Check sfwd_lms_has_access( $course_id, $user_id ) inside the filter and set condition accordingly.

Do custom tabs work in Focus Mode?

Yes. Focus Mode renders the same tabs module, so the filter applies there as well.

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.