uo_generate_course_certificate
Filters the arguments used to generate a course certificate, allowing modification before the PDF is created.
add_filter( 'uo_generate_course_certificate', $callback, 10, 3 );
Description
Filters whether a course certificate should be generated. Allows developers to conditionally prevent certificate generation based on the provided course and user IDs, or modify the arguments passed to the PDF generation function. Fires before the certificate is created.
Usage
add_filter( 'uo_generate_course_certificate', 'your_function_name', 10, 3 );
Parameters
-
$generate_pdf_args(mixed) - This parameter indicates whether a certificate should be generated, defaulting to `true`.
-
$course_id(mixed) - This parameter contains an array of arguments used for generating the PDF certificate.
-
$user_id(mixed) - This parameter contains the ID of the course for which the certificate is being generated.
Return Value
The filtered value.
Examples
<?php
/**
* Example of using the 'uo_generate_course_certificate' filter hook.
* This example demonstrates how to conditionally prevent certificate generation
* for specific courses or users, or modify the arguments passed to the PDF generator.
*/
add_filter( 'uo_generate_course_certificate', 'my_custom_course_certificate_logic', 10, 4 );
/**
* Custom logic for the 'uo_generate_course_certificate' filter.
*
* @param bool $generate_certificate The default boolean indicating whether to generate the certificate.
* @param array $pdf_args The arguments prepared for PDF generation.
* @param int $course_id The ID of the course.
* @param int $user_id The ID of the user.
* @return bool Returns false to prevent certificate generation, or the modified $pdf_args.
*/
function my_custom_course_certificate_logic( $generate_certificate, $pdf_args, $course_id, $user_id ) {
// Example: Do not generate certificates for a specific course (e.g., course ID 123).
if ( $course_id === 123 ) {
error_log( "Skipping certificate generation for course ID {$course_id} for user ID {$user_id}." );
return false; // Prevent certificate generation.
}
// Example: Do not generate certificates for a specific user (e.g., user ID 456).
if ( $user_id === 456 ) {
error_log( "Skipping certificate generation for user ID {$user_id} in course ID {$course_id}." );
return false; // Prevent certificate generation.
}
// Example: Modify PDF generation arguments.
// Let's say we want to add a custom footer to the certificate for premium users.
// We'll assume a function `is_premium_user($user_id)` exists to check this.
if ( function_exists( 'is_premium_user' ) && is_premium_user( $user_id ) ) {
if ( isset( $pdf_args['parameters'] ) && is_array( $pdf_args['parameters'] ) ) {
$pdf_args['parameters']['custom_footer'] = 'Congratulations on completing this premium course!';
error_log( "Adding custom footer for premium user {$user_id} in course {$course_id}." );
}
}
// If no conditions met to prevent generation, return the (potentially modified) arguments.
// The filter expects either a boolean (true to proceed, false to stop) or the arguments to pass to generate_pdf.
// Since we are modifying $pdf_args, we should return it. If we just wanted to control generation,
// we'd return true or false. By returning the array, we're effectively saying "proceed with these args".
return $pdf_args;
}
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/course-completion-certificate.php:304
'save_path' => $save_path,
'user' => $user,
'file_name' => $file_name,
'parameters' => $setup_parameters,
], $course_id, $user_id );
//External override if certificate is not needed!
$uo_generate_course_certs = apply_filters( 'uo_generate_course_certificate', true, $generate_pdf_args, $course_id, $user_id );
if ( ! $uo_generate_course_certs ) {
return;
}
$file = self::generate_pdf( $generate_pdf_args );
//Allow custom Link to an upload folder
$upload_dir = wp_upload_dir();