# LifterLMS: Send Visitors to Login From Lessons

> Redirects logged-out visitors who open a lesson or quiz to the login screen, then back to that lesson once they log in.

- Type: PHP
- Runs: Frontend cl
- Category: [Courses & Community](https://wpide.com/library/category/learning/)
- Tags: lifterlms, login, redirect
- Author: WPIDE Team
- Installs: 0
- Updated: 2026-10-28
- Page: https://wpide.com/library/lifterlms-login-required-for-lessons/

## Code

```php
if ( ! function_exists( 'llms_get_student' ) ) {
    return;
}

add_action( 'template_redirect', function () {
    if ( is_user_logged_in() || ! is_singular( array( 'lesson', 'llms_quiz' ) ) ) {
        return;
    }

    if ( 'yes' === get_post_meta( get_queried_object_id(), '_llms_free_lesson', true ) ) {
        return;
    }

    wp_safe_redirect( wp_login_url( get_permalink() ) );
    exit;
} );
```

## Notes

Free lessons marked as "free lesson" in LifterLMS stay open to everyone.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "LifterLMS: Send Visitors to Login From Lessons" 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/
