uo_download_certificates_in_bulk_pdf_args
Filters arguments for bulk PDF certificate generation to allow modification before processing.
add_filter( 'uo_download_certificates_in_bulk_pdf_args', $callback, 10, 3 );
Description
Allows developers to modify the arguments used when generating bulk PDF certificates. Intercept and adjust parameters such as certificate post type, object ID, and user ID before the PDF generation process begins. This filter is applied before creating the PDF files for bulk certificate downloads.
Usage
add_filter( 'uo_download_certificates_in_bulk_pdf_args', 'your_function_name', 10, 3 );
Parameters
-
$certificate_post(mixed) - This parameter contains the WordPress post object for the certificate.
-
$object_id(mixed) - This parameter refers to the WordPress post object representing the certificate being downloaded.
-
$user_id(mixed) - This parameter contains the ID of the object associated with the certificate, such as a course or lesson.
Return Value
The filtered value.
Examples
/**
* Example of how to hook into 'uo_download_certificates_in_bulk_pdf_args'
* to modify the arguments passed to the PDF generation function.
*
* This example might add a custom watermark to the PDF if a specific
* post type is detected, or change the filename slightly based on user role.
*/
add_filter( 'uo_download_certificates_in_bulk_pdf_args', function( $pdf_args, $object_id, $user_id ) {
// Access the current user object
$user = new WP_User( $user_id );
// Check if the user has a specific role
if ( user_can( $user_id, 'administrator' ) ) {
// Add a custom watermark for administrators
$pdf_args['watermark_text'] = 'Admin Draft';
$pdf_args['watermark_color'] = '#FF0000'; // Red watermark
}
// If the certificate is for a specific quiz (object_id) that requires a different template
if ( $object_id === 123 ) { // Replace 123 with an actual object ID
$pdf_args['template_path'] = WP_PLUGIN_DIR . '/my-custom-plugin/templates/special-certificate-template.html';
}
// Modify the filename if the certificate is for a premium user
if ( user_can( $user_id, 'premium_user' ) ) {
$pdf_args['file_name'] = 'PremiumCertificate-' . $pdf_args['file_name'];
}
// You can also modify the save path if needed
// $pdf_args['save_path'] = WP_CONTENT_DIR . '/uploads/premium-certificates/';
// Important: Always return the modified arguments
return $pdf_args;
}, 10, 3 ); // 10 is the priority, 3 is the number of accepted 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/download-certificates-in-bulk.php:735
public static function generate_group_certificate( $user_id, $object_id, $batch_id, $type = 'group' ) {
$user = new WP_User( $user_id );
$setup_parameters = self::setup_parameters( $object_id, $user_id, $type );
if ( 1 !== (int) $setup_parameters['print-certificate'] ) {
return;
}
self::$current_time_stamp = time();
$new_folder = '/bulk_export_' . $type . '_' . date_i18n( 'Y-m-d' ) . '_' . $batch_id . '/';
$certificate_post = $setup_parameters['certificate-post'];
/* Save Path on Server under Upload & allow overwrite */
$save_path = WP_CONTENT_DIR . apply_filters( 'uo_download_certificates_in_bulk_folder', '/uploads/bulk-certificates' ) . $new_folder;
$uo_dir = WP_CONTENT_DIR . apply_filters( 'uo_download_certificates_in_bulk_folder', '/uploads/bulk-certificates' );
if ( ! is_dir( $uo_dir ) ) {
mkdir( $uo_dir, 0755 );
}
/**
* New filter added so that arguments can be passed. Adding arguments
* to previous filter above might break sites since
* there might be no argument supplied with override function
*
* @since 3.6.4
* @author Saad
* @var $save_path
*/
$save_path = apply_filters( 'uo_download_certificates_in_bulk_upload_dir', $save_path, $user, $object_id, $certificate_post, self::$current_time_stamp );
/* Creating a fileName that is going to be stored on the server. Certificate-QUIZID-USERID-NONCE_String */
$file_name = sanitize_title( $user->user_email . '-' . $object_id . '-' . $certificate_post . '-' . date_i18n( 'Y-m-d', self::$current_time_stamp ) . '-' . wp_create_nonce( self::$current_time_stamp ) );
//Allow overwrite of custom filename
$file_name = apply_filters( 'uo_download_certificates_in_bulk_filename', $file_name, $user, $object_id, $certificate_post, self::$current_time_stamp );
if ( ! file_exists( $save_path ) && ! mkdir( $save_path, 0755 ) && ! is_dir( $save_path ) ) { // phpcs:ignore
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_download_certificates_in_bulk_pdf_args',
array(
'certificate_post' => $certificate_post,
'save_path' => $save_path,
'user' => $user,
'file_name' => $file_name,
'parameters' => $setup_parameters,
'type' => $type,
'bulk_generator' => 'yes',
),
$object_id,
$user_id
);
self::generate_pdf( $generate_pdf_args, $type );
}