Depending on your business model, you may allow lifetime access to the course content after your students complete the course.
Maybe so they can revisit the lessons and boost their learning experience.
Or simply as a good will bonus, and allow them to see course updates in the long run.
On the other hand, you can also set an access expiration instead.
LearnDash provides a shortocde [ld_course_expire_status]
.
In short, it allows you to show your students their current access status. If the access has not expired yet, they’ll see the date until they have access to.
So far, so good.
But what if you want to show them a custom message like “You have 90 days left before your access expires”.
And each time they log into their course the number of days left is reduced accordingly until the expiration.
You’d need to create your own custom solution to show a custom expiration message like that.
So, the logic would work something like this:
- Retrieve the access expiration timestamp.
- Get the current time.
- Calculate the difference between each timestamp in seconds.
- Transform the resulting time difference to days.
- Evalute if the returned difference is negative, then indicate the course has expired.
- Build a default return value including the difference in days.
The code would be something as follows:
function ld_days_until_course_expiration( $user_id, $course_id ) {
// Retrieve the timestamp for when the course access will expire.
$expires_on_timestamp = ld_course_access_expires_on( $course_id, $user_id );
// Return a message when the course does not have an expiration timestamp.
if ( empty( $expires_on_timestamp ) ) {
return __( 'Course access does not expire.', 'learndash' );
}
// Get the current time.
$current_time = current_time( 'timestamp' );
// Calculate the difference in seconds.
$time_difference = $expires_on_timestamp - $current_time;
// Convert the time difference to days.
$days_left = floor( $time_difference / ( 24 * 60 * 60 ) );
// Indicate the course has expired if the difference is negative.
if ( $days_left <= 0 ) {
return __( 'Course access has expired.', 'learndash' );
}
// Default message
$message = sprintf( _n( 'There is %d day left before course access expires.', 'There are %d days left before course access expires.', $days_left, 'learndash' ), $days_left );
/**
* Filters the course expiration message.
*
* @param string $message Default expiration message.
* @param int $days_left Number of days left.
* @param int $user_id User ID.
* @param int $course_id Course ID.
*/
return apply_filters( 'ld_course_expiration_message', $message, $days_left, $user_id, $course_id );
}
You can see in the return statement the returned value is wrapped in the apply_filters() WordPress function.
That allows you to filter the message later.
Let’s say you want to display a custom message when the user’s access is to expire in 10 days.
Maybe to create a scarcity sense and let them know they should renew their subscription.
You can do something like this:
function custom_ld_course_expiration_message( $message, $days_left, $user_id, $course_id ) {
if ( $days_left == 10 ) {
return __( 'Only 10 days left! Consider renewing or upgrading your course.', 'learndash' );
}
return $message;
}
add_filter( 'ld_course_expiration_message', 'custom_ld_course_expiration_message', 10, 4 );
The most basic usage, whatever you choice is, is simply echoing the ld_days_until_course_expiration() function like this:
add_action('wp_head', function(){
echo ld_days_until_course_expiration( '2', '7' ); // Display the access expiration for a user with ID 2 enrolled in a course with ID 7.
});
Keep in mind the ld_days_until_course_expiration() function requires the user ID and the course ID you are checking the access expiration status for.
To make it happen dynamically, you can do the following:
- Use the get_current_user_id() WordPress function to retrieve the current user ID.
- Build a wrapper around get_the_ID(), to get the current post ID if it is a course.
That’s it. Now over to you.
Let me know in the comments if you have any ideas to add, or make any questions if something is not clear.
Until next time!