LearnDash: Course Status Badge Shortcode
Adds a [wpide_ld_status] shortcode that shows a small "Not started", "In progress" or "Completed" badge for the current student and course.
Opens WPIDE on
Code
if ( ! defined( 'LEARNDASH_VERSION' ) ) {
return;
}
add_shortcode( 'wpide_ld_status', function ( $atts ) {
$atts = shortcode_atts( array( 'course_id' => 0 ), $atts, 'wpide_ld_status' );
$user_id = get_current_user_id();
$course_id = $atts['course_id'] ? absint( $atts['course_id'] ) : (int) learndash_get_course_id( get_the_ID() );
if ( ! $user_id || ! $course_id || ! sfwd_lms_has_access( $course_id, $user_id ) ) {
return '';
}
$progress = learndash_course_progress( array( 'user_id' => $user_id, 'course_id' => $course_id, 'array' => true ) );
$percent = (int) ( $progress['percentage'] ?? 0 );
if ( learndash_course_completed( $user_id, $course_id ) || $percent >= 100 ) {
$state = array( 'is-completed', 'Completed' );
} elseif ( $percent > 0 ) {
$state = array( 'is-in-progress', 'In progress' );
} else {
$state = array( 'is-not-started', 'Not started' );
}
return '<span class="wpide-ld-status ' . esc_attr( $state[0] ) . '" style="display:inline-block;padding:.15em .6em;border-radius:999px;border:1px solid currentColor;font-size:.85em">' . esc_html( $state[1] ) . '</span>';
} );
Notes
Style it with the classes .wpide-ld-status.is-not-started, .is-in-progress and .is-completed.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.