uo_group_completion_certificate_filename
Filters the filename for a group completion certificate before it's generated.
add_filter( 'uo_group_completion_certificate_filename', $callback, 10, 5 );
Description
Filters the filename for group completion certificates. Developers can modify the generated filename using this hook, allowing for custom naming conventions based on user, group, certificate, and timestamp data. The default filename includes email, group ID, certificate post, date, and a nonce.
Usage
add_filter( 'uo_group_completion_certificate_filename', 'your_function_name', 10, 5 );
Parameters
-
$file_name(mixed) - This parameter holds the dynamically generated filename for the group completion certificate.
-
$user(mixed) - This parameter is used to define the filename of the generated group completion certificate.
-
$group_id(mixed) - This parameter contains the WordPress user object for the user who has completed the group.
-
$certificate_post(mixed) - This parameter represents the ID of the group for which the completion certificate is being generated.
-
$current_time_stamp(mixed) - This parameter contains a timestamp representing the current time, used to include a date in the certificate filename.
Return Value
The filtered value.
Examples
<?php
/**
* Example function to modify the group completion certificate filename.
* This function adds the user's display name to the filename for better identification.
*
* @param string $file_name The original generated filename.
* @param WP_User $user The WP_User object of the completing user.
* @param int $group_id The ID of the group.
* @param WP_Post $certificate_post The WP_Post object representing the certificate.
* @param int $current_time_stamp The current timestamp.
* @return string The modified filename.
*/
function my_custom_group_certificate_filename( $file_name, $user, $group_id, $certificate_post, $current_time_stamp ) {
// Get the user's display name, falling back to username if display name is empty.
$user_display_name = ! empty( $user->display_name ) ? $user->display_name : $user->user_login;
// Sanitize the display name to be safe for use in a filename.
$sanitized_display_name = sanitize_title( $user_display_name );
// Construct the new filename with the user's display name.
// Example: username-groupid-certificateid-date-nonce becomes:
// displayname-username-groupid-certificateid-date-nonce
$new_file_name = $sanitized_display_name . '-' . $file_name;
return $new_file_name;
}
add_filter( 'uo_group_completion_certificate_filename', 'my_custom_group_certificate_filename', 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/group-completion-certificate.php:288
$group_cert_meta = '_uo-group-cert-' . $group_id;
/* Creating a fileName that is going to be stored on the server. Certificate-QUIZID-USERID-NONCE_String */
$file_name = sanitize_title( $user->user_email . '-' . $group_id . '-' . $certificate_post . '-' . date( 'Ymd', self::$current_time_stamp ) . '-' . wp_create_nonce( self::$current_time_stamp ) );
//Allow overwrite of custom filename
$file_name = apply_filters( 'uo_group_completion_certificate_filename', $file_name, $user, $group_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;