LearnDash: Certificate Download Button Shortcode
Adds a [wpide_ld_certificate course_id="123"] shortcode that shows a "Download your certificate" button once the student has completed the course, and nothing before that.
Opens WPIDE on
Code
if ( ! defined( 'LEARNDASH_VERSION' ) ) {
return;
}
add_shortcode( 'wpide_ld_certificate', function ( $atts ) {
$atts = shortcode_atts( array( 'course_id' => 0, 'label' => 'Download your certificate' ), $atts, 'wpide_ld_certificate' );
$user_id = get_current_user_id();
$course_id = $atts['course_id'] ? absint( $atts['course_id'] ) : (int) learndash_get_course_id( get_the_ID() );
if ( ! $user_id || ! $course_id ) {
return '';
}
$link = learndash_get_course_certificate_link( $course_id, $user_id );
if ( ! $link ) {
return '';
}
return '<a class="wpide-ld-certificate wp-element-button" href="' . esc_url( $link ) . '" target="_blank" rel="noopener">' . esc_html( $atts['label'] ) . '</a>';
} );
Notes
Inside a course or lesson the course_id can be left out. Change the button text with label="...".
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.