# LearnDash: Send Visitors to Login From Lessons

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

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

## Code

```php
if ( ! defined( 'LEARNDASH_VERSION' ) ) {
    return;
}

add_action( 'template_redirect', function () {
    if ( is_user_logged_in() || ! is_singular( array( 'sfwd-lessons', 'sfwd-topic', 'sfwd-quiz' ) ) ) {
        return;
    }

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

## Notes

Course pages stay public so visitors can still see what they would be buying.

## How to use it

Install the free WPIDE plugin (https://wordpress.org/plugins/wpide/), find "LearnDash: 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/
