Skip to content

Instantly share code, notes, and snippets.

@j2machado
Last active January 25, 2025 03:38
Show Gist options
  • Save j2machado/7f7c00fdfe2dcb0b13d4f7eb33ba3d9f to your computer and use it in GitHub Desktop.
Save j2machado/7f7c00fdfe2dcb0b13d4f7eb33ba3d9f to your computer and use it in GitHub Desktop.
Debug LearnDash Quiz Certificates
<?php
/**
* Debug LearnDash Quiz Certificates.
*
* Enter the Quiz ID and User ID into the [debug_learndash_quiz_certificate] shortcode.
* If the user has earned the certificate for the quiz, the shortcode will return
* the Certificate Download link.
*
* Clicking the link should open the generated certificate PDF according to the
* certificate settings in the quiz.
*
* Example of usage:
*
* [debug_learndash_quiz_certificate quiz_id="123" user_id="321"]
*
* Replace quiz_id="123" and user_id="321" with the correct IDs.
*/
/**
* Debug a LearnDash quiz certificate for any user.
*
* @param array $atts Shortcode attributes.
* @return string Certificate link HTML.
*/
add_shortcode( 'debug_any_user_quiz_certificate', function ( $atts ) {
$atts = shortcode_atts(
array(
'quiz_id' => 0,
'user_id' => 0,
),
$atts
);
$quiz_id = intval( $atts['quiz_id'] );
$user_id = intval( $atts['user_id'] );
return debug_learndash_certificate( $quiz_id, $user_id );
} );
/**
* Debug a LearnDash quiz certificate for any user.
*
* @param int $quiz_post_id The LearnDash Quiz post ID.
* @param int $user_id The user ID to check certificate for.
*
* @return string The certificate link HTML if available.
*/
function debug_learndash_quiz_certificate( $quiz_post_id, $user_id ) {
// Ensure quiz post ID and user ID are integers to prevent errors.
$quiz_post_id = intval( $quiz_post_id );
$user_id = intval( $user_id );
// Check if the quiz post exists and is of the correct type.
$quiz_post = get_post( $quiz_post_id );
if ( ! ( $quiz_post instanceof WP_Post ) || 'sfwd-quiz' !== $quiz_post->post_type ) {
return 'Invalid quiz post ID.';
}
// Fetch the certificate details for the specified user.
$certificate_details = learndash_certificate_details( $quiz_post_id, $user_id );
// Check if certificate details are available.
if ( empty( $certificate_details ) || empty( $certificate_details['certificateLink'] ) ) {
return 'No certificate available for this user and quiz.';
}
// Return the certificate link.
return "<a href='{$certificate_details['certificateLink']}'>Print Certificate</a>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment