Tutor LMS: Course Progress Bar Shortcode
Adds a [wpide_tutor_progress] shortcode that shows the logged-in student's completion percentage for a Tutor LMS course as an accessible progress bar.
Opens WPIDE on
Code
if ( ! function_exists( 'tutor_utils' ) ) {
return;
}
add_shortcode( 'wpide_tutor_progress', function ( $atts ) {
$atts = shortcode_atts( array( 'course_id' => 0 ), $atts, 'wpide_tutor_progress' );
$user_id = get_current_user_id();
$course_id = $atts['course_id'] ? absint( $atts['course_id'] ) : ( get_post_type() === tutor()->course_post_type ? get_the_ID() : 0 );
if ( ! $user_id || ! $course_id || ! tutor_utils()->is_enrolled( $course_id, $user_id ) ) {
return '';
}
$percent = (int) tutor_utils()->get_course_completed_percent( $course_id, $user_id );
return sprintf(
'<div class="wpide-tutor-progress"><div role="progressbar" aria-valuenow="%1$d" aria-valuemin="0" aria-valuemax="100" aria-label="%2$s" style="height:10px;border-radius:5px;background:rgba(127,127,127,.25);overflow:hidden"><div style="width:%1$d%%;height:100%%;background:currentColor"></div></div><p style="margin:.4em 0 0;font-size:.9em">%3$s</p></div>',
$percent,
esc_attr( get_the_title( $course_id ) . ' progress' ),
esc_html( sprintf( '%d%% complete', $percent ) )
);
} );
Notes
On a course page it uses that course; elsewhere add course_id="123".
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.