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"]
