ucec_pdf_file_name
Filters the PDF file name for certificates, allowing customization of the filename.
add_filter( 'ucec_pdf_file_name', $callback, 10, 4 );
Description
Filters the name of the generated PDF certificate file. Developers can modify the default naming convention, which includes certificate ID, user email, and timestamp, to customize file names for better organization or specific needs.
Usage
add_filter( 'ucec_pdf_file_name', 'your_function_name', 10, 4 );
Parameters
-
$cert_name(mixed) - This parameter is the default generated filename for the PDF certificate, which will be modified by the filter.
-
$certificate_id(mixed) - This parameter contains the generated PDF file name, which can be modified by the filter.
-
$current_user(mixed) - This parameter holds the unique identifier for the certificate being generated.
-
$timestamp(mixed) - This parameter provides the current WordPress user object, which can be used to access user-specific data like their email address for inclusion in the filename.
Return Value
The filtered value.
Examples
/**
* Example filter to modify the PDF certificate file name.
*
* This function appends the course title to the default file name structure.
*
* @param string $cert_name The original generated certificate file name.
* @param int $certificate_id The ID of the certificate post.
* @param WP_User $current_user The WordPress user object.
* @param int $timestamp The current timestamp.
*
* @return string The modified certificate file name.
*/
add_filter( 'ucec_pdf_file_name', function( $cert_name, $certificate_id, $current_user, $timestamp ) {
// Attempt to get the course ID from the certificate post meta.
// This assumes the course ID is stored in a meta field named 'ucec_course_id'.
// You might need to adjust the meta key based on how your plugin stores this.
$course_id = get_post_meta( $certificate_id, 'ucec_course_id', true );
if ( ! empty( $course_id ) && 0 !== $course_id ) {
$course_title = get_the_title( $course_id );
if ( ! empty( $course_title ) ) {
// Append a sanitized version of the course title to the file name.
$cert_name .= '-' . sanitize_title( $course_title );
}
}
return $cert_name;
}, 10, 4 );
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/award-certificate.php:765
public function setup_variables( $certificate_id, $current_user, $course_id ) {
$timestamp = current_time( 'timestamp' );
$cert_name = "$certificate_id-" . sanitize_title( $current_user->user_email ) . '-' . $timestamp;
$cert_name = apply_filters( 'ucec_pdf_file_name', $cert_name, $certificate_id, $current_user, $timestamp );
return array(
'certificate_post' => $certificate_id,
'save_path' => $this->save_path,
'user' => $current_user,
'file_name' => sanitize_title( $cert_name ),
'parameters' => array(
'userID' => $current_user->ID,
'course-id' => ( 0 !== $course_id ) ? $course_id : $certificate_id,
'course-name' => get_the_title( $course_id ),
'print-certificate' => 1,
),
);
}