Adil Elsaeed

Senior WordPress Developer

IT Consultant

Freelancer

Adil Elsaeed

Senior WordPress Developer

IT Consultant

Freelancer

Blog Post

LearnDash Certificates REST API

31 August,2021 WordPress
LearnDash Certificates REST API

LearnDash WordPress plugin has an excellent REST API but version 2 is still in beta so there are some missed routes and endpoints one of them is certificate routes which means until now you can’t download a certificate through your mobile app without writing a custom code handle serving user certificates list and files from the server, so I wrote this snippet which creates a custom REST API routes for LearnDash user certificates for one of my client’s project and I like to share it with you.

Register Certificates REST Route

First, we need to create a route to get a list of user certificates through the URL:

http://yoursite.com/wp-json/yourproject/v1/certificates

by using the WordPress function register_rest_route and other learndash functions.

add_action( 'rest_api_init', function () {
  register_rest_route( 'yourproject/v1', '/certificates', array(
    'methods' => 'GET',
    'callback' => 'yourproject_rest_certificates',
    'permission_callback' => '__return_true'
  ) );
} );

function yourproject_rest_certificates(WP_REST_Request $request ){
    /* GET Certificates For Courses*/
    $post_args = array(
        'post_type'      => 'sfwd-courses',
        'posts_per_page' => - 1,
        'post_status'    => 'publish'
    );

    
    $courses         = get_posts( $post_args );
    $show_cert_title = true;
    $certificate_list = [];

    foreach ( $courses as $course ) {
        $certificate_id     = learndash_get_setting( $course->ID, 'certificate' );
        $certificate_object = get_post( $certificate_id );
        if ( ! empty( $certificate_object ) ) {
            if ( 'on' === $show_cert_title ) {
                $certificate_title = $certificate_object->post_title;
            } else {
                $certificate_title = $course->post_title;
            }
            $certificate_link = learndash_get_course_certificate_link( $course->ID, get_current_user_id() );

            if ( $certificate_link && '' !== $certificate_link ) {
                $date_earned                     = learndash_user_get_course_completed_date( get_current_user_id(), $course->ID );
                $certificate_list[ $certificate_id ] = [
                    'title'       => $certificate_title,
                    'course_id'   => $course->ID,
                    'date_earned' => $date_earned,
                    'link'        => $certificate_link
                ];
            }
        }
    }

    return $certificate_list;
}

Now we have a list of all user certificates, the next step is to get a single certificate PDF file.

REST Route for Single Certificate

This route will return a certificate file when you send a GET request including Certificate ID and Course ID to the URL:

http://yoursite.com/wp-json/yourproject/v1/certificate

add_action( 'rest_api_init', function () {
  register_rest_route( 'yourproject/v1', '/certificate', array(
    'methods' => 'GET',
    'callback' => 'yourproject_rest_certificate',
    'permission_callback' => '__return_true'
  ) );
} );

function yourproject_rest_certificate(WP_REST_Request $request){
    $cert_id = $request['cert_id'];
    $course_id = $request['course_id'];
    $user_id = get_current_user_id();
    $cert_post = get_post( $cert_id );
    $parsed = parse_blocks( $cert_post->post_content );
    $service = new PDF();
    return $service->serve( $parsed, $cert_id, $cert_id );
}

Note that we use Learndash PDF instance so you need to add this line on the top of your PHP file

use LearnDash_Certificate_Builder\Component\PDF;

And we assume you are using Learndash Certificate Builder.

Taggs:
Open chat
1
Hello👋
Can I help you?