ucec_generate_quiz_certificate_content
function modify_pdf_certificate_content( $cert_content, $user_id, $quiz_id, $course_id ){ //enter your modifications or use regex to modify content return $cert_content; } add_action( 'ucec_generate_quiz_certificate_content', 'modify_pdf_certificate_content', 20, 4 ); Filters the content of a generated quiz certificate, allowing customization before it's displayed or saved.
add_filter( 'ucec_generate_quiz_certificate_content', $callback, 10, 2 );
Description
Filters the content for quiz certificates before generation. Developers can modify the HTML content using this hook to customize text, add dynamic data, or change styling. It provides access to the certificate content, user, quiz ID, and additional parameters for advanced customization.
Usage
add_filter( 'ucec_generate_quiz_certificate_content', 'your_function_name', 10, 2 );
Parameters
-
$cert_content(mixed) - - **$user** `mixed`
-
$quiz_id(mixed) - - **$parameters** `mixed`
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to use the 'ucec_generate_quiz_certificate_content' filter hook.
*
* This example demonstrates modifying the certificate content to add a custom message
* based on the user's score for a specific quiz.
*/
add_filter( 'ucec_generate_quiz_certificate_content', 'my_custom_certificate_message', 20, 4 );
/**
* Adds a custom message to the certificate based on the quiz score.
*
* @param string $cert_content The current certificate content.
* @param int $user_id The ID of the user who completed the quiz.
* @param int $quiz_id The ID of the quiz.
* @param array $parameters An array of parameters, potentially including the quiz result.
* @return string The modified certificate content.
*/
function my_custom_certificate_message( $cert_content, $user_id, $quiz_id, $parameters ) {
// Check if the result parameter exists and is a number.
if ( isset( $parameters['result'] ) && is_numeric( $parameters['result'] ) ) {
$score = (float) $parameters['result'];
// Add a congratulatory message if the score is 90% or higher.
if ( $score >= 90 ) {
$custom_message = "nnCongratulations on your outstanding performance! You've demonstrated exceptional knowledge.";
$cert_content .= $custom_message;
}
// Add a message encouraging further study if the score is below 60%.
elseif ( $score < 60 ) {
$custom_message = "nnKeep up the great work! Consider reviewing the material to further enhance your understanding.";
$cert_content .= $custom_message;
}
}
// You can also add other dynamic content here based on user_id, quiz_id, etc.
// For example, to add the current date in a specific format:
// $current_date = date( 'Y-m-d H:i:s' );
// $cert_content .= "nnGenerated on: " . $current_date;
return $cert_content;
}
?>
Placement
This code should be placed in the functions.php file of your active theme, a custom plugin, or using a code snippets plugin.
Source Code
src/includes/tcpdf-certificate-code.php:893
public static function generate_quiz_content( $cert_content, $args ) {
$parameters = $args['parameters'];
$completion_time = $args['completion_time'];
$user = isset( $args['current_user'] ) ? $args['current_user'] : wp_get_current_user();
$quiz_id = $args['quiz_id'];
preg_match_all( '/[quizinfo(.+?)]/', $cert_content, $matches );
if ( $matches ) {
foreach ( $matches[0] as $quizinfo ) {
if ( strpos( $quizinfo, 'timestamp' ) ) {
$qinfo = str_replace( 'show="timestamp"', '', $quizinfo );
preg_match( '/"(.*)"/', $qinfo, $date_format );
if ( $date_format ) {
$date = learndash_adjust_date_time_display( $completion_time, $date_format[1] );
} else {
$date = learndash_adjust_date_time_display( $completion_time, 'F d, Y' );
}
$cert_content = str_ireplace( $quizinfo, $date, $cert_content );
}
if ( strpos( $quizinfo, 'timespent' ) ) {
$cert_content = str_ireplace( $quizinfo, learndash_seconds_to_time( $parameters['timespent'] ), $cert_content );
}
if ( strpos( $quizinfo, 'percentage' ) ) {
$cert_content = str_ireplace( $quizinfo, $parameters['result'], $cert_content );
}
if ( strpos( $quizinfo, 'points' ) ) {
$cert_content = str_ireplace( $quizinfo, $parameters['points'], $cert_content );
}
if ( strpos( $quizinfo, 'total_points' ) ) {
$cert_content = str_ireplace( $quizinfo, $parameters['correctQuestions'], $cert_content );
}
if ( strpos( $quizinfo, 'pass' ) ) {
$cert_content = str_ireplace( $quizinfo, 'Yes', $cert_content );
}
if ( strpos( $quizinfo, 'count' ) ) {
$cert_content = str_ireplace( $quizinfo, $parameters['points'], $cert_content );
}
if ( strpos( $quizinfo, 'score' ) ) {
$cert_content = str_ireplace( $quizinfo, $parameters['points'], $cert_content );
}
if ( strpos( $quizinfo, 'quiz_title' ) ) {
$cert_content = str_ireplace( $quizinfo, get_the_title( $quiz_id ), $cert_content );
}
if ( strpos( $quizinfo, 'course_title' ) ) {
$cert_content = str_ireplace( $quizinfo, get_the_title( $parameters['course-id'] ), $cert_content );
}
}
}
$cert_content = preg_replace( '/([usermeta)/', '[usermeta user_id="' . $user->ID . '" ', $cert_content );
$cert_content = preg_replace( '/([quizinfo)/', '[quizinfo user_id="' . $user->ID . '" quiz="' . $quiz_id . '" ', $cert_content );
/**
*
* function modify_pdf_certificate_content( $cert_content, $user_id, $quiz_id, $course_id ){
* //enter your modifications or use regex to modify content
* return $cert_content;
* }
*
* add_action( 'ucec_generate_quiz_certificate_content', 'modify_pdf_certificate_content', 20, 4 );
*/
$cert_content = apply_filters( 'ucec_generate_quiz_certificate_content', $cert_content, $user->ID, $quiz_id, $parameters['course-id'] );
return $cert_content;
}