Custom Additional Template Locations

Our Themer's Guide details how you can override one or more of the many template files that make up our various views. But you aren't limited to just those views.  After you've created your own template file, place it within a custom plugin folder or your top-level theme directory (to apply even if you switch themes).

With the snippet below, you can point the file loader to look in additional locations:

<?php

/**
 * The Events Calendar and related plugins: Add your own location for template file loading.
 *
 * Example "Single Event within List View" for Avada theme (which has theme overrides).
 * Now tries to load in this order:
 * This plugin:                                  /app/public/wp-content/plugins/my-plugin/tribe-events/list/single-event.php
 * Theme root so it stays even if switch themes: /app/public/wp-content/themes/tribe-events/list/single-event.php
 * Child theme with theme overrides:             /app/public/wp-content/themes/Avada-Child-Theme/tribe-events/list/single-event.php
 * Parent theme with theme overrides:            /app/public/wp-content/themes/Avada/tribe-events/list/single-event.php
 * Plugin default:                               /app/public/wp-content/plugins/the-events-calendar/src/views/list/single-event.php
 *
 * @link https://gist.github.com/b76421f2490a8b8995493f203e11b331
 *
 * @see \Tribe__Events__Templates::getTemplateHierarchy()
 *
 * @param string $file     The full file path trying to be loaded.
 * @param string $template The template name, such as
 *
 * @return string
 */
function tribe_additional_template_locations( string $file, string $template ) {
	// Put them in the order of priority (first location gets loaded before second location if first exists)
	$new_locations = [
		'my_plugin'   => trailingslashit( plugin_dir_path( __FILE__ ) ) . 'tribe-events', // /app/public/wp-content/plugins/cliff-test-tec/tribe-events/default-template.php
		'themes_root' => trailingslashit( get_theme_root() ) . 'tribe-events', // /app/public/wp-content/themes/tribe-events/default-template.php
	];

	foreach ( $new_locations as $location ) {
		$new_file = trailingslashit( $location ) . $template;
		if ( file_exists( $new_file ) ) {
			return $new_file;
		}
	}

	return $file;
}

add_filter( 'tribe_events_template', 'tribe_additional_template_locations', 10, 2 );

(View code on Gist)

As with all of our recipes, please note that we share this in the hope it will be useful but without any guarantees or commitments. If you wish to use it, it is your responsibility to test it first of all and adapt it to your needs (or find someone who can do so on your behalf). We are unable to provide further support in relation to this recipe.