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.
Opens WPIDE on
Code
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 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.