Halloween Sale 15% off Ends in See the plans
PHP

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.

Don’t have WPIDE yet? Install it on your site

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

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.