This week we were tasked with producing a number of new course certificate templates for our newest client. Many of their courses are training qualification based and therefore have an expiry date of one year before the learner is required to retake a refresher course and exam.
The client therefore needed a way for this expiry date to be displayed on the certificate, with the date set 365 days after the course completion date (variable across all users).
LearnDash however does not (currently) provide a shortcode for dynamically generating a course completion date + additional days in order to create this expiry date value.
Thankfully however, Patrick Kellogg had provided a PHP Function + Shortcode solution which he’d shared earlier on the official LearnDash Tips And Tricks Facebook Group for another user.
We went ahead and tested this code ourselves and found that it worked a treat! I then decided to write this blog post in order to share the code (slightly modified for our needs) so that it may easily be found in the future. This is provided below.
I’ll restate for the record that this code is not our own and we take no credit for having shared this. Patrick insisted that anyone finding this useful donate $5 to his favourite charity, and in case you are reading this yourself one day Patrick I just want to say thanks and that I went ahead and did this myself today!
Add the following function code to your functions.php file in your theme/child theme folder:
(Just make sure to update the ‘365’ value if you need your expiry date to be more or less than one year. The date format can also be tweaked by editing the PHP date code values.)
function pfk_bonus_courseinfo( $attr ) { global $wpdb; $shortcode_atts = shortcode_atts ( array( 'show' => '', //[score], [count], [pass], [rank], [timestamp], [pro_quizid], [points], [total_points], [percentage], [timespent] 'user_id' => '', 'quiz' => '', 'time' => '', 'format' => "d-m-Y, g:i a" ), $attr); extract( $shortcode_atts ); $time = ( empty( $time ) && isset( $_REQUEST['time'] ))? $_REQUEST[ 'time' ]:$time; $show = ( empty( $show ) && isset( $_REQUEST[ 'show'] ))? $_REQUEST[ 'show' ]:$show; $user_id = ( empty( $user_id ) && isset( $_REQUEST[ 'user_id' ] ))? $_REQUEST[ 'user_id' ]:$user_id; $course_id = ( empty( $course_id ) && isset( $_REQUEST[ 'course_id' ]))? $_REQUEST[ 'course_id' ]:$course_id; if( empty( $user_id )) $user_id = get_current_user_id(); if( empty( $course_id ) || empty( $user_id ) || empty( $show )) return ""; $courseinfo = get_user_meta( $user_id, "_sfwd-courses", true ); if (!empty ( $courseinfo ) ) { foreach( $courseinfo as $course_i ) { if( isset( $course_i[ 'time' ]) && $course_i[ 'time' ] == $time && $course_i[ 'course_id' ] == $course_id) { $selected_courseinfo = $course_i; break; } if( $course_i[ 'course_id' ] == $course_id) $selected_courseinfo2 = $course_i; } $selected_courseinfo = empty( $selected_courseinfo)? $selected_courseinfo2:$selected_courseinfo; } switch ( $show ) { case 'expiration_date_3': $string_date = do_shortcode('[#courseinfo show="completed_on" format="d-m-Y"]'); //DELETE THE # FROM THIS LINE $time = strtotime($string_date); $newEndingDate = date("d-m-Y", strtotime(date("Y-m-d", $time) . " + 365 day")); return apply_filters('learndash_courseinfo', $newEndingDate, $shortcode_atts); case 'expiration_date_3_euro': $string_date = do_shortcode('[#courseinfo show="completed_on" format="d-m-Y"]'); //DELETE THE # FROM THIS LINE $time = strtotime($string_date); $newEndingDate = date("d-m-Y", strtotime(date("d-m-Y", $time) . " + 365 day")); return apply_filters('learndash_courseinfo', $newEndingDate, $shortcode_atts); } } add_shortcode( 'pfk_bonus_courseinfo', 'pfk_bonus_courseinfo' );
Then use the following shortcode to display this date on your certificate:
[pfk_bonus_courseinfo show="expiration_date_3"]
Sweet!
Hi there,
This code is showing ’03-02-1970′ as new expiry date ?
Course publishing date is : 2021/04/23
This is a date format issue, but how to rectify this ?
please suggest
Hi Kashi,
You can edit the format of the date published by tweaking the PHP date values in the code.
e.g. editing the ‘d-m-Y’ value to the format you require.
https://www.php.net/manual/en/datetime.format.php
Hi Chris,
Thank you for this, I have it working but on my certificate the date is saying – 01-01-1971 and not the expiry date do you know why that would be?
Also this shows for certificates given on course completion but not for assessment completion is there a way to show for assessment certificates also?
Can I also use the value from this shortcode to add to a variable that I can them use in a few other queries / php equations?
Lastly can I use this shortcode elsewhere on the site to show access to the course for example on the profile page to show when their certification expires and can I pair the date and time with the course name e.g ‘ Your access to [course name] expires on – ‘
Hi Chris
These are all good questions, and all I can really offer are some theories to hopefully help you on your way!:
I would check the date format used on your platform to make sure that this matches the same format used in the function code. If there are differences then it may be causing the issues with the expiry date calculation as you are noticing.
I’m sure it should also be possible to reuse the custom in other functions like you have described, because of the fact that it is made available site-wide through the theme functions.php file.
Lastly I’m not sure if the shortcode can be used in other ways. It might be possible, but you may have to feed in the course_id number for the specific course into the function in another way.
Hi Chris,
Thank you for your reply, I’ve got it working now.
I’ll take a look at that and try feeding in the course Id to the query would thee be a way to query for all courses?
So query initially for all courses and then foreach course if there is a certificate awarded for this user show expiration date, is this possible and do you know how I would go about doing that or where to start?
Chris
Hi Chris,
I have the same problem with 01-01-1971 showing on my certificate.
Tried to change date to match my platform and operating system but the certificate does not show a proper date.
Also tried to use the custom date format from https://www.php.net/manual/en/datetime.format.php but it will not show the
current date just format display changes.
Sadly, my PHP knowledge is limited so I might be doing something wrong.
How did you solve your issue?
I am using WP 5.8.1. with Astra theme on Win10.
Hope you can help.
Hi Chris,
I have the same problem with 01-01-1971 showing on my certificate.
Tried to change date to match my platform and operating system but the certificate does not show a proper date.
Also tried to use the custom date format from https://www.php.net/manual/en/datetime.format.php but it will not show the
current date just format display changes.
Sadly, my PHP knowledge is limited so I might be doing something wrong.
How did you solve your issue?
I am using WP 5.8.1. with Astra theme on Win10.
Hope you can help.
Mike,
I’ve just realised what is happening! My site is converting some of the code in the snippet above in the frontend because it’s thinking that it is a shortcode on the page! DOH!
I’ll see what I can do about this to prevent it from happening and will update the code in just a minute.
Chris
Hi Chris,
thank you very much ! We got it working now.
Appreciate it.
Have a nice day 🙂
Mike
Thank you very much for this post. Unfortunately, the code won’t work. The date is shown.
01-01-1971
How can I fix this problem?
Kris,
Please read through the code and make sure you are removing the # symbols as instructed and checking that quotation marks have been pasted in the correct format etc.
Chris
Thank you now everything is working fine 🙂
Hi Chris,
I was wondering how to go about having various shortcodes for 1,3 and 5 years? I know very little about PHP, but given you call the expiration_date_3, would I just create another 4 case statements (expiration_date_3years, expiration_date_3years_euro, expiration_date_5years, expiration_date_5years_euro) copy those in the switch ($show) edit the days and the code would still be valid? Thanks.
You can create a different shortcode for each of the year values you need. Just make sure to adjust the 365 value displayed twice in the demo code above, which is what sets the expiry date to one year after the course was completed in this case.
So could I just expand on the switch statement only (rather than copying the full function 3 times)?
switch ( $show ) {
case ‘expiration_date_3’:
…+ 1095 day
case ‘expiration_date_3_euro’:
…+ 1095 day
case ‘expiration_date_1’:
…+ 365 day
case ‘expiration_date_1_euro’:
…+ 365 day
case ‘expiration_date_5’:
…+ 1825 day
case ‘expiration_date_5_euro’:
…+ 1825 day
}
and than just call it like this?
[pfk_bonus_courseinfo show=”expiration_date_3″]
[pfk_bonus_courseinfo show=”expiration_date_1″]
[pfk_bonus_courseinfo show=”expiration_date_5″]
This does indeed sound logical so creating a new case with something like:
case ‘expiration_date_3’:
$string_date = do_shortcode(‘[#courseinfo show=”completed_on” format=”d-m-Y”]’); //DELETE THE # FROM THIS LINE
$time = strtotime($string_date);
$newEndingDate = date(“d-m-Y”, strtotime(date(“Y-m-d”, $time) . ” + 1095 day”));
break;
I only now realized, this only works for courses, not for quizzes :(. How would I make it work for quizzes. I can pass the quiz_id, that is not the problem, but it seems that no part of this code check for the quiz pass date.