uo_quiz_completion_certificate_filename
Filters the filename for a quiz completion certificate before it's generated.
add_filter( 'uo_quiz_completion_certificate_filename', $callback, 10, 5 );
Description
Filters the filename for generated quiz completion certificates. Use this hook to dynamically alter the certificate's file name before it's saved. Parameters include the current filename, user, quiz ID, certificate post, and timestamp for granular control.
Usage
add_filter( 'uo_quiz_completion_certificate_filename', 'your_function_name', 10, 5 );
Parameters
-
$file_name(mixed) - This parameter contains the filename for the quiz completion certificate, which can be filtered and modified.
-
$current_user(mixed) - This parameter contains the generated filename for the quiz completion certificate, which can be modified by the filter.
-
$quiz_id(mixed) - This parameter contains the current WordPress user object, providing access to their information.
-
$certificate_post(mixed) - This parameter contains the ID of the quiz for which the certificate is being generated.
-
$current_time_stamp(mixed) - This parameter contains the post object representing the generated certificate, which can be used to extract information for constructing the filename.
Return Value
The filtered value.
Examples
/**
* Modify the completion certificate filename to include a date and time prefix.
*
* This filter allows developers to customize the filename of the generated
* quiz completion certificate. This example adds a date and time prefix
* to ensure uniqueness and chronological order.
*
* @param string $file_name The original filename.
* @param WP_User $current_user The current logged-in user object.
* @param int $quiz_id The ID of the quiz.
* @param WP_Post $certificate_post The post object for the certificate template.
* @param string $current_time_stamp The current timestamp string (e.g., 'Y-m-d H:i:s').
*
* @return string The modified filename.
*/
add_filter( 'uo_quiz_completion_certificate_filename', function( $file_name, $current_user, $quiz_id, $certificate_post, $current_time_stamp ) {
// Get the formatted date and time for the prefix
$date_prefix = date( 'Ymd_His_' );
// Append the date prefix to the existing filename
$modified_file_name = $date_prefix . $file_name;
// Ensure the filename is still sanitized after modification
return sanitize_file_name( $modified_file_name );
}, 10, 5 );
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:126
$certificate_post = $setup_parameters['certificate-post'];
$save_path = apply_filters( 'uo_quiz_certificate_save_path', WP_CONTENT_DIR . '/uploads/user-certificates/' );
$completion_time = self::$current_time_stamp;
$quiz_title = html_entity_decode( get_the_title( $post->ID ) );
/* Creating a fileName that is going to be stored on the server. Certificate-QUIZID-USERID-NONCE_String */
$file_name = sanitize_title( $current_user->ID . '-' . $quiz_title . '-' . wp_create_nonce( $completion_time ) );
$file_name = apply_filters( 'uo_quiz_completion_certificate_filename', $file_name, $current_user->ID, $quiz_id, $certificate_post, self::$current_time_stamp );
if ( ! file_exists( $save_path ) && ! mkdir( $save_path, 0755 ) && ! is_dir( $save_path ) ) {
throw new RuntimeException( sprintf( 'Directory "%s" was not created', $save_path ) );
}
$generate_pdf_args = apply_filters(
'uo_quiz_completion_generate_pdf_args',