Filter uncanny-toolkit-pro

uo_course_cert_img_path

Filters the image path for course certificates, allowing modification before it's used.

add_filter( 'uo_course_cert_img_path', $callback, 10, 2 );

Description

Filters the path or URL for the course certificate image. Developers can modify the returned value to specify whether to return the file path or the image URL for the certificate background. The hook passes the current path type, post ID, and thumbnail ID.


Usage

add_filter( 'uo_course_cert_img_path', 'your_function_name', 10, 2 );

Parameters

$post_id (mixed)
This parameter represents the desired path type for the certificate image, defaulting to 'path' but allowing filters to change it to 'url' to retrieve the image URL instead.
$thumbnail_id (mixed)
This parameter represents the ID of the post for which the certificate image path is being determined.

Return Value

The filtered value.


Examples

<?php
/**
 * Modify the course certificate image path to point to a custom directory.
 *
 * This filter allows developers to change the default location where the
 * certificate image is retrieved from. For example, you might want to
 * store custom certificate images in a theme-specific directory.
 *
 * @param mixed   $path       The default path or URL.
 * @param int     $post_id    The ID of the course post.
 * @param int     $thumbnail_id The ID of the featured image (thumbnail).
 * @return string The modified image path.
 */
add_filter( 'uo_course_cert_img_path', function( $path, $post_id, $thumbnail_id ) {
	// Check if it's a valid course post and if a thumbnail is set.
	if ( ! $post_id || ! $thumbnail_id ) {
		return $path; // Return the default if no post or thumbnail.
	}

	// Define a custom directory within the uploads folder for certificates.
	$custom_cert_dir = 'custom-certificates';

	// Get the attachment data for the thumbnail.
	$attachment = get_post( $thumbnail_id );

	// If the attachment exists and is an image.
	if ( $attachment && wp_attachment_is_image( $attachment->ID ) ) {
		// Construct the custom path.
		$upload_dir = wp_upload_dir();
		$custom_path = trailingslashit( $upload_dir['basedir'] ) . $custom_cert_dir . '/' . basename( get_post_meta( $thumbnail_id, '_wp_attached_file', true ) );

		// Check if the custom path exists. If it does, use it.
		if ( file_exists( $custom_path ) ) {
			return $custom_path;
		}
	}

	// Fallback to the default path if the custom one doesn't exist or conditions aren't met.
	return $path;
}, 10, 3 );

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

public static function learndash_get_thumb_path( $post_id ) {
		$thumbnail_id = get_post_thumbnail_id( $post_id );
		$path_type    = apply_filters( 'uo_course_cert_img_path', 'path', $post_id, $thumbnail_id );
		if ( 'url' === $path_type ) {
			return get_the_post_thumbnail_url( get_post( $post_id ), 'full' );
		}
		$img_path              = get_post_meta( $thumbnail_id, '_wp_attached_file', true );
		$upload_url            = wp_upload_dir();
		$upload_url['basedir'] = str_replace( '\', '/', $upload_url['basedir'] );

		return $upload_url['basedir'] . '/' . $img_path;
	}

Scroll to Top