Halloween Sale 15% off Ends in See the plans
PHP

Tutor LMS: CSS Classes Based on the Student's Course Status

Adds body classes such as wpide-tutor-enrolled or wpide-tutor-completed on Tutor LMS course pages, so you can show or hide elements with CSS.

Don’t have WPIDE yet? Install it on your site

Code

if ( ! function_exists( 'tutor_utils' ) ) {
    return;
}

add_filter( 'body_class', function ( $classes ) {
    if ( ! is_singular( tutor()->course_post_type ) ) {
        return $classes;
    }

    $course_id = get_queried_object_id();
    $user_id   = get_current_user_id();

    if ( ! $user_id ) {
        $classes[] = 'wpide-tutor-guest';
    } elseif ( ! tutor_utils()->is_enrolled( $course_id, $user_id ) ) {
        $classes[] = 'wpide-tutor-not-enrolled';
    } else {
        $classes[] = 'wpide-tutor-enrolled';

        if ( tutor_utils()->is_completed_course( $course_id, $user_id ) ) {
            $classes[] = 'wpide-tutor-completed';
        }
    }

    return $classes;
} );

Notes

Classes: wpide-tutor-guest, wpide-tutor-not-enrolled, wpide-tutor-enrolled, wpide-tutor-completed.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.