# 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.

- Type: PHP
- Runs: Run everywhere
- Category: [Courses & Community](https://wpide.com/library/category/learning/)
- Tags: tutor, shortcode, access
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/tutor-enrolled-content-shortcode/

## Code

```php
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 use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "Tutor LMS: Enrolled-Students-Only Shortcode" in its snippet library and add it in one click; or create a new PHP snippet in WPIDE (or any code snippets plugin) and paste the code above. More snippets: https://wpide.com/library/
