Tutor LMS: Enrolled-Students-Only Shortcode
Adds a [wpide_tutor_enrolled course_id="123"] shortcode that shows its content only to students enrolled in that course, with an optional message for others.
Opens WPIDE on
Code
if ( ! function_exists( 'tutor_utils' ) ) {
return;
}
add_shortcode( 'wpide_tutor_enrolled', function ( $atts, $content = '' ) {
$atts = shortcode_atts( array( 'course_id' => 0, 'message' => '' ), $atts, 'wpide_tutor_enrolled' );
$course_id = absint( $atts['course_id'] );
$user_id = get_current_user_id();
if ( $course_id && $user_id && ( tutor_utils()->is_enrolled( $course_id, $user_id ) || current_user_can( 'edit_post', $course_id ) ) ) {
return do_shortcode( $content );
}
return $atts['message'] ? '<p class="wpide-tutor-locked">' . esc_html( $atts['message'] ) . '</p>' : '';
} );
Notes
Example: [wpide_tutor_enrolled course_id="123" message="Enroll to get the slides."]Slides link[/wpide_tutor_enrolled].
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.