LearnDash: Body Classes for Course Status
Adds body classes such as wpide-ld-enrolled and wpide-ld-completed on course, lesson and topic pages, so you can style or hide things with CSS depending on the student's status.
Opens WPIDE on
Code
if ( ! defined( 'LEARNDASH_VERSION' ) ) {
return;
}
add_filter( 'body_class', function ( $classes ) {
if ( ! is_singular( array( 'sfwd-courses', 'sfwd-lessons', 'sfwd-topic', 'sfwd-quiz' ) ) ) {
return $classes;
}
$user_id = get_current_user_id();
$course_id = (int) learndash_get_course_id( get_queried_object_id() );
if ( ! $user_id ) {
$classes[] = 'wpide-ld-guest';
} elseif ( ! $course_id || ! sfwd_lms_has_access( $course_id, $user_id ) ) {
$classes[] = 'wpide-ld-not-enrolled';
} else {
$classes[] = 'wpide-ld-enrolled';
if ( learndash_course_completed( $user_id, $course_id ) ) {
$classes[] = 'wpide-ld-completed';
}
}
return $classes;
} );
Notes
Classes: wpide-ld-guest, wpide-ld-not-enrolled, wpide-ld-enrolled and wpide-ld-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.