ulgm_template_path
Filters the director path to the template file This can be used for template overrides by modifying the path to go to a directory in the theme or another plugin. Filters the template directory path before loading a template file, allowing for theme or plugin overrides.
add_filter( 'ulgm_template_path', $callback, 10, 2 );
Description
Filters the template directory path, enabling theme or plugin template overrides. Developers can modify the `$templates_directory` parameter to point to custom template locations, ensuring their modified templates are loaded instead of the plugin's defaults.
Usage
add_filter( 'ulgm_template_path', 'your_function_name', 10, 2 );
Parameters
-
$templates_directory(string) - Path to the plugins template folder
-
$file_name(string) - The file name of the template file
Return Value
The filtered value.
Examples
/**
* Example filter for 'ulgm_template_path' to override template location.
*
* This example demonstrates how to check if a template exists in the theme's
* 'templates/my-plugin-templates' directory and uses that if available,
* otherwise falls back to the plugin's default path.
*/
add_filter(
'ulgm_template_path',
function( $templates_directory, $file_name ) {
// Define a custom directory within the theme for overrides.
$theme_override_dir = trailingslashit( get_stylesheet_directory() ) . 'templates/my-plugin-templates/';
// Construct the potential override template path.
$override_template_path = $theme_override_dir . $file_name;
// Check if the override template file exists.
if ( file_exists( $override_template_path ) ) {
// If it exists, return the theme override path.
return trailingslashit( $theme_override_dir );
}
// If the override doesn't exist, return the original plugin path.
return $templates_directory;
},
10, // Priority: default is 10
2 // Accepted args: $templates_directory and $file_name
);
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/class-utilities.php:488
public static function get_template( $file_name ) {
$template_path = apply_filters( 'uncanny_groups_template_path', 'uncanny-groups' . DIRECTORY_SEPARATOR );
$asset_path = self::locate_template( $template_path . $file_name );
if ( empty( $asset_path ) ) {
$templates_directory = ULGM_ABSPATH . 'src/templates' . DIRECTORY_SEPARATOR;
/**
* Filters the director path to the template file
*
* This can be used for template overrides by modifying the path to go to a directory in the theme or another plugin.
*
* @param string $templates_directory Path to the plugins template folder
* @param string $file_name The file name of the template file
*
* @since 1.0.0
*/
$templates_directory = apply_filters( 'ulgm_template_path', $templates_directory, $file_name );
$asset_path = $templates_directory . $file_name;
}
return $asset_path;
}