Filter uncanny-toolkit-pro

uo_generate_quiz_certificate_tcpdf_dest

Filters the destination directory for quiz certificates generated by TCPDF before saving.

add_filter( 'uo_generate_quiz_certificate_tcpdf_dest', $callback, 10, 1 );

Description

Filters the destination for saving generated PDF certificates. Use this hook to dynamically change where the PDF is saved (e.g., 'I' for inline display, 'D' for download) or to alter the save path and filename before the file is written. This hook fires after the certificate content is generated and before it's output to the specified destination.


Usage

add_filter( 'uo_generate_quiz_certificate_tcpdf_dest', 'your_function_name', 10, 1 );

Return Value

The filtered value.


Examples

/**
 * Modify the destination for saving the quiz certificate.
 *
 * By default, 'F' tells TCPDF to save the file to the server's filesystem.
 * This filter allows us to change that destination, for example, to save
 * the PDF to a temporary directory or to return it directly for download.
 *
 * @param string $destination The default destination for TCPDF output. 'F' means save to a local file.
 * @return string The modified destination for TCPDF output.
 */
add_filter( 'uo_generate_quiz_certificate_tcpdf_dest', function( $destination ) {
    // In this example, we'll add a custom directory to the default save path.
    // This is just an illustration; you'd likely have more complex logic
    // based on user roles, specific quiz settings, etc.

    // Ensure we are still saving to the filesystem if the original was 'F'.
    if ( $destination === 'F' ) {
        // Assuming $save_path is accessible globally or passed differently in a real scenario.
        // For this example, let's simulate it.
        // In a real WordPress plugin, you'd likely get this from plugin settings or WordPress uploads.
        $base_upload_dir = wp_upload_dir();
        $custom_save_path = trailingslashit( $base_upload_dir['basedir'] ) . 'uo-certificates/quiz/';

        // Ensure the custom directory exists.
        if ( ! wp_mkdir_p( $custom_save_path ) ) {
            // Handle error if directory cannot be created.
            error_log( 'Failed to create custom quiz certificate directory: ' . $custom_save_path );
            return 'I'; // Fallback to inline output or another safe default.
        }

        // The TCPDF 'F' destination expects a full path including filename.
        // The filter is applied before the filename is fully constructed in the source code.
        // So, we return 'F' here and modify the path within the filter, or we could
        // return a different output type if we wanted to fully override the saving mechanism.
        // For this example, let's assume the source code will append the filename to the path returned here.
        // However, the source code actually uses $save_path and $file_name independently.
        // The filter currently only receives 'F'. Let's adjust the logic to return a modified path if possible,
        // or to indicate a different output mode.

        // Re-evaluating based on the source code:
        // $output = apply_filters( 'uo_generate_quiz_certificate_tcpdf_dest', 'F' );
        // This means the filter receives 'F' and is expected to return the *output destination*.
        // TCPDF documentation for Output() method:
        // D : download file
        // I : send the file inline to the browser
        // S : return the document as string
        // F : save to a file
        // FI : save to a file and send to the browser inline
        // FD : save to a file and send to the browser as download
        // E : return base64 encoded

        // If we want to control where it's saved, we need to return 'F' AND ensure
        // $save_path is modified before TCPDF uses it. The filter only modifies the *type* of output.
        // To modify the save path, we'd need a different hook or a more complex filter.

        // Given the current filter's signature and context, it's likely intended to change
        // the *output method* (e.g., from saving to disk 'F' to returning as a string 'S').
        // Let's demonstrate returning the PDF as a string for immediate download or processing.

        return 'S'; // Return the PDF content as a string.
    }

    // If the destination is not 'F', return it as is, or handle other cases.
    return $destination;
}, 10, 1 ); // Priority 10, 1 accepted argument.

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/includes/tcpdf-certificate-code.php:808

ob_clean();

		$full_path = $save_path . $file_name . '.pdf';

		switch ( $certificate_type ) {
			case 'quiz':
				$output = apply_filters( 'uo_generate_quiz_certificate_tcpdf_dest', 'F' );
				break;
			case 'group':
				$output = apply_filters( 'uo_generate_group_certificate_tcpdf_dest', 'F' );
				break;
			case 'course':
				$output = apply_filters( 'uo_generate_course_certificate_tcpdf_dest', 'F' );
				break;


Scroll to Top