Filter uncanny-toolkit-pro

uo_single_{$name}_template

Filters the file path used to render a single post template, allowing customization of the template file.

add_filter( 'uo_single_{$name}_template', $callback, 10, 1 );

Description

This filter allows developers to override the default template file path for Uncanny Owl's lesson and topic grids. By returning a new file path, you can customize the presentation of these elements. The hook fires after Uncanny Owl determines the default template, providing an opportunity for modification.


Usage

add_filter( 'uo_single_{$name}_template', 'your_function_name', 10, 1 );

Parameters

$filepath (mixed)
This parameter contains the file path of the template being used.

Return Value

The filtered value.


Examples

add_filter( 'uo_single_lesson_template', 'my_custom_lesson_template_filter', 10, 2 );

/**
 * Custom filter to override the default Uncanny Automator lesson template.
 *
 * This function demonstrates how to conditionally load a different template file
 * for single lesson views based on custom logic, perhaps to integrate with
 * another plugin or display content differently.
 *
 * @param string $filepath The current file path of the lesson template.
 * @param string $name     The name of the template (e.g., 'lesson').
 * @return string The modified file path to the custom lesson template.
 */
function my_custom_lesson_template_filter( $filepath, $name ) {
	// Only apply this filter to 'lesson' templates.
	if ( 'lesson' !== $name ) {
		return $filepath;
	}

	// Example: Check if a specific option is enabled to use a custom template.
	// In a real scenario, you might fetch this from WordPress options or a custom setting.
	$use_custom_template = get_option( 'my_plugin_enable_custom_lesson_template', false );

	if ( ! $use_custom_template ) {
		return $filepath; // Return the original path if the custom template isn't enabled.
	}

	// Define the path to your custom lesson template.
	// This assumes your custom template is located in your theme's directory.
	$custom_template_path = get_template_directory() . '/templates/my-custom-lesson-template.php';

	// Check if the custom template file actually exists before returning its path.
	if ( file_exists( $custom_template_path ) ) {
		return $custom_template_path;
	}

	// If the custom template doesn't exist, fall back to the original filepath.
	return $filepath;
}

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/lesson-topic-grid.php:250

public static function replace_lesson_topic_template( $filepath, $name, $args, $echo, $return_file_path ) {

		// Bail if not the templates we're looking for.
		$template_names = array( 'course', 'lesson' );
		if ( ! in_array( $name, $template_names, true ) ) {
			return $filepath;
		}

		// Check if we have replace values set to on.
		$replace_key = 'course' === $name ? 'lessons' : 'topics';
		$replace     = self::get_settings_value( "uncanny-lesson-grid-replace-{$replace_key}", __CLASS__ );

		// Bail if not replacing.
		if ( 'on' !== $replace ) {
			return $filepath;
		}

		$template = 'legacy';
		if ( class_exists( 'LearnDash_Theme_Register' ) ) {
			$template = LearnDash_Theme_Register::get_active_theme_key();
		}

		// Get the file path.
		$file     = 'legacy' === $template ? "single-{$name}.php" : "single-ld30-{$name}.php";
		$filepath = self::get_template( $file, dirname( dirname( __FILE__ ) ) . '/src' );

		// Return the filtered file path.
		return apply_filters( "uo_single_{$name}_template", $filepath );
	}

Scroll to Top