uo_generate_quiz_certificate
Filters the arguments used to generate a quiz certificate, allowing modification before PDF creation.
add_filter( 'uo_generate_quiz_certificate', $callback, 10, 3 );
Description
This filter hook allows developers to influence whether a quiz certificate is generated. It fires just before the PDF generation process. Developers can return `false` to prevent certificate creation or modify the arguments passed to the PDF generation function, such as the certificate's content or recipient details, by manipulating `$generate_pdf_args`.
Usage
add_filter( 'uo_generate_quiz_certificate', 'your_function_name', 10, 3 );
Parameters
-
$generate_pdf_args(mixed) - This parameter indicates whether the certificate generation process should proceed, with `true` meaning it should, and `false` meaning it should be skipped.
-
$quiz_id(mixed) - This parameter contains an array of arguments used to generate the PDF certificate, including completion time, setup parameters, and user information.
-
$current_user(mixed) - This parameter holds the ID of the quiz for which the certificate is being generated.
Return Value
The filtered value.
Examples
<?php
/**
* Filter the 'uo_generate_quiz_certificate' hook to conditionally prevent PDF generation
* if the quiz is marked as "draft" or if a specific user role is not met.
*
* @param bool $generate_pdf The default value indicating whether to generate the PDF (true).
* @param array $pdf_args An array of arguments used to generate the PDF.
* @param int $quiz_id The ID of the quiz.
* @param int $user_id The ID of the current user.
* @return bool Returns false to prevent PDF generation, true otherwise.
*/
add_filter( 'uo_generate_quiz_certificate', function( $generate_pdf, $pdf_args, $quiz_id, $user_id ) {
// Prevent PDF generation for draft quizzes.
if ( get_post_status( $quiz_id ) === 'draft' ) {
return false;
}
// Conditionally prevent PDF generation for users not in a specific role.
$user = wp_get_current_user();
if ( ! in_array( 'editor', (array) $user->roles ) ) {
return false;
}
// If all conditions pass, allow PDF generation.
return $generate_pdf;
}, 10, 4 );
?>
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/classes/generate-p-d-f-email.php:146
'completion_time' => $completion_time,
'parameters' => $setup_parameters,
'current_user' => $current_user,
'user' => $current_user,
)
);
$uo_generate_quiz_certs = apply_filters( 'uo_generate_quiz_certificate', true, $generate_pdf_args, $quiz_id, $current_user->ID );
if ( $uo_generate_quiz_certs ) {
$file = self::generate_pdf( $generate_pdf_args );
}
if ( apply_filters( 'uo_quiz_completion_add_certificate_attached', true ) ) {
$email_params['msg'] .= "nn";