Filter Since 3.6.4 uncanny-toolkit-pro

uo_course_certificate_upload_dir

New filter added so that arguments can be passed. Adding arguments to previous filter above might break sites since there might be no argument supplied with override function Filters the directory where course certificates are saved, allowing modification of the save path based on user, course, certificate, and current time.

add_filter( 'uo_course_certificate_upload_dir', $callback, 10, 3 );

Description

Filters the directory where course certificates are saved. This hook provides access to the user, course ID, certificate post object, and timestamp, allowing developers to dynamically modify the save path for enhanced customization or integration. Use with caution to avoid breaking existing installations.


Usage

add_filter( 'uo_course_certificate_upload_dir', 'your_function_name', 10, 3 );

Parameters

$save_path (mixed)
- **$user** `mixed`
$course_id (mixed)
- **$certificate_post** `mixed`
$current_time_stamp (mixed)

Return Value

The filtered value.


Examples

<?php
/**
 * Example of how to hook into the 'uo_course_certificate_upload_dir' filter.
 *
 * This example demonstrates how to change the default upload directory for course certificates.
 * We'll append a year-based sub-directory to the existing save path.
 */
add_filter( 'uo_course_certificate_upload_dir', 'my_custom_certificate_upload_dir', 10, 5 );

/**
 * Custom function to modify the certificate upload directory.
 *
 * @param string $save_path         The original save path.
 * @param WP_User $user             The user object.
 * @param int    $course_id         The course ID.
 * @param WP_Post $certificate_post The certificate post object.
 * @param int    $current_time_stamp The current time as a timestamp.
 * @return string The modified save path.
 */
function my_custom_certificate_upload_dir( $save_path, $user, $course_id, $certificate_post, $current_time_stamp ) {
    // Ensure we have a valid user object before proceeding.
    if ( ! $user instanceof WP_User ) {
        return $save_path;
    }

    // Get the current year.
    $current_year = date( 'Y', $current_time_stamp );

    // Append the year-based sub-directory to the save path.
    // We use trailingslashit to ensure proper directory separation.
    $modified_save_path = trailingslashit( $save_path ) . trailingslashit( $current_year );

    // You could also add other dynamic elements, e.g., user ID or course ID:
    // $modified_save_path = trailingslashit( $save_path ) . trailingslashit( $current_year ) . trailingslashit( $user->ID ) . trailingslashit( $course_id );

    // It's good practice to ensure the directory exists. The plugin likely handles this,
    // but if you're creating new subdirectories, you might want to ensure they are writable.
    // For example:
    // if ( ! wp_mkdir_p( $modified_save_path ) ) {
    //     // Handle error if directory creation fails.
    //     error_log( 'Failed to create certificate upload directory: ' . $modified_save_path );
    //     return $save_path; // Return original path if creation fails.
    // }

    return $modified_save_path;
}

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:278

* to previous filter above might break sites since
		 * there might be no argument supplied with override function
		 *
		 * @since  3.6.4
		 * @author Saad
		 * @var $save_path
		 */
		$save_path = apply_filters( 'uo_course_certificate_upload_dir', $save_path, $user, $course_id, $certificate_post, self::$current_time_stamp );

		$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


Scroll to Top