Filter uncanny-toolkit-pro

uo_course_completion_certificate_filename

Filters the filename for a course completion certificate before it's generated.

add_filter( 'uo_course_completion_certificate_filename', $callback, 10, 5 );

Description

Filter the filename generated for course completion certificates before it's saved. Modify the user's email, course ID, certificate post, and timestamp for custom naming conventions. This hook fires after a base filename is generated but before it's finalized.


Usage

add_filter( 'uo_course_completion_certificate_filename', 'your_function_name', 10, 5 );

Parameters

$file_name (mixed)
This parameter holds the dynamically generated filename for the course completion certificate, which is sanitized for use as a file name.
$user (mixed)
This parameter holds the generated filename for the course completion certificate, which is then sanitized for storage on the server.
$course_id (mixed)
This parameter contains the user object associated with the course completion.
$certificate_post (mixed)
This parameter contains the ID of the course for which the certificate is being generated.
$current_time_stamp (mixed)
This parameter contains the current timestamp formatted as 'YYYYmd', likely used to uniquely identify the certificate file.

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to use the 'uo_course_completion_certificate_filename' filter hook.
 *
 * This example adds a prefix to the default filename, making it more descriptive.
 *
 * @param string $file_name        The default filename generated by the plugin.
 * @param WP_User $user            The user object for whom the certificate is generated.
 * @param int $course_id           The ID of the course.
 * @param int $certificate_post_id The ID of the certificate post.
 * @param int $current_time_stamp  The current timestamp.
 * @return string The modified filename.
 */
add_filter( 'uo_course_completion_certificate_filename', function( $file_name, $user, $course_id, $certificate_post_id, $current_time_stamp ) {

	// Ensure we have a valid user object.
	if ( ! is_a( $user, 'WP_User' ) ) {
		return $file_name;
	}

	// Construct a more descriptive filename.
	// Example: 'custom_prefix-username-courseid-certid-timestamp.pdf'
	// We'll append '.pdf' as the default already includes it.
	$username = $user->user_login;
	$prefix = 'my-lms-certificate';

	// Extract the original file extension if it exists, otherwise assume .pdf
	$extension = pathinfo( $file_name, PATHINFO_EXTENSION );
	if ( empty( $extension ) ) {
		$extension = 'pdf';
	}

	// Remove the original extension to avoid double extensions.
	$base_name = basename( $file_name, '.' . $extension );

	$custom_file_name = sanitize_title( $prefix . '-' . $username . '-' . $course_id . '-' . $certificate_post_id . '-' . date( 'YmdHis', $current_time_stamp ) ) . '.' . $extension;

	// The original $file_name likely already has a nonce and a date.
	// We are opting for a different structure here, so we replace it.
	return $custom_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/course-completion-certificate.php:286

$course_cert_meta = '_uo-course-cert-' . $course_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 . '-' . $course_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_course_completion_certificate_filename', $file_name, $user, $course_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;


Scroll to Top