uo_course_completion_generate_pdf_args
Filters the arguments used to generate a course completion PDF, before the PDF is created.
add_filter( 'uo_course_completion_generate_pdf_args', $callback, 10, 3 );
Description
Filters arguments used to generate course completion PDFs. Developers can modify the PDF's certificate post data, save path, user details, and filename before PDF generation. This hook fires after the PDF save path is determined and before the PDF is generated.
Usage
add_filter( 'uo_course_completion_generate_pdf_args', 'your_function_name', 10, 3 );
Parameters
-
$certificate_post(mixed) - The `$certificate_post` parameter contains the WordPress post object representing the certificate template being used.
-
$course_id(mixed) - This parameter contains the WordPress post object representing the certificate template.
-
$user_id(mixed) - This parameter contains the ID of the course for which the completion certificate is being generated.
Return Value
The filtered value.
Examples
<?php
/**
* Example of how to modify the arguments passed to the PDF generation for course completion.
* This example adds a custom header to the PDF, if a specific course ID is detected.
*
* @param array $generate_pdf_args The arguments array for PDF generation.
* @param int $course_id The ID of the course.
* @param int $user_id The ID of the user completing the course.
* @return array Modified arguments for PDF generation.
*/
add_filter( 'uo_course_completion_generate_pdf_args', function( $generate_pdf_args, $course_id, $user_id ) {
// Define a specific course ID for which we want to apply custom settings.
$target_course_id = 123;
// Check if the current course ID matches our target course ID.
if ( $course_id === $target_course_id ) {
// Add a custom header to the PDF generation parameters.
// This assumes the PDF generation library or a subsequent filter can interpret this.
// You might need to adjust 'custom_header_text' based on how the PDF generation
// process expects custom data.
if ( isset( $generate_pdf_args['parameters'] ) && is_array( $generate_pdf_args['parameters'] ) ) {
$generate_pdf_args['parameters']['custom_header_text'] = __( 'Congratulations on completing ' . get_the_title( $course_id ), 'your-text-domain' );
} else {
// If 'parameters' doesn't exist or isn't an array, initialize it.
$generate_pdf_args['parameters'] = [
'custom_header_text' => __( 'Congratulations on completing ' . get_the_title( $course_id ), 'your-text-domain' ),
];
}
// Optionally, you could also modify the filename or save path for this specific course.
// $generate_pdf_args['file_name'] = 'special-certificate-' . $user_id . '-' . $course_id . '.pdf';
// $generate_pdf_args['save_path'] = WP_CONTENT_DIR . '/uploads/special-certificates/';
}
// Always return the (potentially modified) arguments.
return $generate_pdf_args;
}, 10, 3 ); // Priority 10, accepts 3 arguments.
?>
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:295
throw new RuntimeException( sprintf( 'Directory "%s" was not created', $save_path ) );
}
$full_path = $save_path . $file_name;
self::$pdf_filename = $full_path;
//Allow PDF args to be modified
$generate_pdf_args = apply_filters( 'uo_course_completion_generate_pdf_args', [
'certificate_post' => $certificate_post,
'save_path' => $save_path,
'user' => $user,
'file_name' => $file_name,
'parameters' => $setup_parameters,
], $course_id, $user_id );