LearnDash: Show Students When Their Course Access Expires
Shows a notice at the top of course, lesson and topic pages telling enrolled students the date their access ends, for courses that use access expiration.
Opens WPIDE on
Code
if ( ! defined( 'LEARNDASH_VERSION' ) ) {
return;
}
add_filter( 'the_content', function ( $content ) {
if ( ! is_singular( array( 'sfwd-courses', 'sfwd-lessons', 'sfwd-topic' ) ) || ! in_the_loop() || ! is_user_logged_in() ) {
return $content;
}
$course_id = (int) learndash_get_course_id( get_the_ID() );
$expires = $course_id ? (int) ld_course_access_expires_on( $course_id, get_current_user_id() ) : 0;
if ( ! $expires || $expires < time() ) {
return $content;
}
$soon = $expires - time() < 7 * DAY_IN_SECONDS;
$notice = sprintf(
'<p class="wpide-ld-expiry%1$s" role="status" style="padding:.6em 1em;border-left:4px solid %2$s;background:rgba(127,127,127,.08)">%3$s</p>',
$soon ? ' is-soon' : '',
$soon ? '#d63638' : '#2271b1',
esc_html( sprintf( 'Your access to this course ends on %s.', wp_date( get_option( 'date_format' ), $expires ) ) )
);
return $notice . $content;
}, 5 );
Notes
Only shows for courses with "Expire access" turned on. The notice turns into a warning in the last 7 days.
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.