Setting up LearnDash shortcodes
In this blog post, we'll walk you through the steps to set up course_completed_check and topic_completed_check Learndash shortcodes for course completion and discuss their functionality.
Published 31 May 2023

LearnDash is a powerful learning management system (LMS) for WordPress that provides many opportunities for customisation. Two of these customisations are shortcodes that allow you to check if a user has completed a course or topic. In this blog post, we’ll walk you through the steps to set up course_completed_check
and topic_completed_check
LearnDash shortcodes for course completion and discuss their functionality.
What are shortcodes?
In WordPress, a shortcode is a small piece of code that you can use inside your posts and pages to perform a specific function. It can also display a certain feature. LearnDash includes several built-in shortcodes, but you can also create custom shortcodes to meet your specific needs.
Setting up the course_completed_check shortcode
The course_completed_check
shortcode will return ‘1’ if the currently logged-in user has completed a specified course, and ‘0’ if they have not. Here’s how to set it up:
function course_completed_check_shortcode( $atts ) { $atts = shortcode_atts( array( 'course_id' => 0, ), $atts ); $course_id = (int) $atts['course_id']; if ( is_user_logged_in() && !empty( $course_id ) ) { $user_id = get_current_user_id(); if ( learndash_course_completed( $user_id, $course_id ) ) { return '1'; } } return '0'; } add_shortcode( 'course_completed_check', 'course_completed_check_shortcode' );
To use this shortcode, simply add [course_completed_check course_id="123"]
to your post or page, replacing “123” with the ID of the course you want to check.
Setting Up the topic_completed_check Shortcode
The LearnDash shortcode topic_completed_check
shortcode works similarly to course_completed_check
, but checks if a user has completed a specific topic instead of a course. Here’s how to set it up:
function topic_completed_check_shortcode( $atts ) { $atts = shortcode_atts( array( 'topic_id' => 0, 'course_id' => 0, ), $atts ); $topic_id = (int) $atts['topic_id']; $course_id = (int) $atts['course_id']; if ( is_user_logged_in() && !empty( $topic_id ) && !empty( $course_id ) ) { $user_id = get_current_user_id(); if ( learndash_is_topic_complete( $user_id, $topic_id, $course_id ) ) { return '1'; } } return '0'; } add_shortcode( 'topic_completed_check', 'topic_completed_check_shortcode' );
To use this LearnDash shortcode, add [topic_completed_check topic_id="456" course_id="123"]
to your post or page, replacing “456” with the ID of the topic and “123” with the ID of the course you want to check.
How The LearnDash Shortcodes Can Be Useful
These shortcodes are powerful tools for enhancing the learning experience on your site. They can be used to:
- Display customised content: Show specific content to users based on their course or topic completion status. For example, you could use these LearnDash shortcodes to display a congratulatory message or additional resources when a user completes a course or topic. By using conditional logic with a page builder like beaver builder you can display elements if users has completed specific courses or topics.
- Control access to content: Restrict access to certain content based on whether a user has completed a course or topic. For example, you could lock advanced content until the user has completed prerequisite topics or courses.
Troubleshooting Tips
While setting up and using these shortcodes, you might encounter issues. Here are some common problems and their solutions:
- The shortcode always returns ‘0’: This could be because the user is not logged in, the course or topic ID is not set, or the user has not completed the course or topic. Ensure that you are testing the shortcode with a logged-in user. They must also have access to the course or topic and have a correct course or topic ID.
- The shortcode doesn’t work or causes an error: This could be due to you making a typo or syntax error in the shortcode function. It could also be because the LearnDash shortcode function you are using does not exist or is not available. Make sure you’ve copied the code correctly and that you’re using the latest version of LearnDash.
- The shortcode doesn’t display anything: This could be because the shortcode you are using is not being processed by WordPress. Make sure you’re inserting the LearnDash shortcodes in a location where shortcodes are processed. This is often the main content of a post or page.
LeanDash shortcodes a powerful customisation
Custom LearnDash shortcodes like course_completed_check
and topic_completed_check
provide a powerful way to customise your learning platform. Whether you want to display personalised content, track user progress, or control access to materials based on course or topic completion, these shortcodes can help. By understanding how to set them up and use them effectively, you enhance your learners’ experience of your LMS. Contact us if you have any questions about how this can be used to optimise your LMS.
Book a free demo today and let us discover how we can serve your company.