Skip to content

Instantly share code, notes, and snippets.

@cliffordp
Last active Feb 10, 2020
Embed
What would you like to do?
The Events Calendar and related plugins: Add your own location for template file loading.
<?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 );
@mike-weiner

This comment has been minimized.

Copy link

@mike-weiner mike-weiner commented Feb 2, 2020

This is amazing!

Do you know how to achieve this with the new v2 calendar views introduced within v5.0+?

@cliffordp

This comment has been minimized.

Copy link
Owner Author

@cliffordp cliffordp commented Feb 4, 2020

Hi, @mike-weiner - Did you try it with V2 and it didn't work but it did before the upgrade?

@mike-weiner

This comment has been minimized.

Copy link

@mike-weiner mike-weiner commented Feb 4, 2020

@cliffordp Correct.

The Events Calendar v5.0+ introduced new calendar views. When you activate the new calendar views, this code snippet does not modify the new tooltip. If you leave the v2 views disabled on v5.0+, this code snippet still works.

@cliffordp

This comment has been minimized.

Copy link
Owner Author

@cliffordp cliffordp commented Feb 6, 2020

I confirmed that overriding v2 views requires different code. I'll create a new snippet today or tomorrow and post it to https://theeventscalendar.com/knowledgebase/k/customizing-template-files/, and I'll comment here when ready as well.

@mike-weiner

This comment has been minimized.

Copy link

@mike-weiner mike-weiner commented Feb 6, 2020

You rock!
Thank you!

@cliffordp

This comment has been minimized.

Copy link
Owner Author

@cliffordp cliffordp commented Feb 7, 2020

I updated https://theeventscalendar.com/knowledgebase/k/custom-additional-template-locations/ for V2 (direct link is https://gist.github.com/cliffordp/39e68939132bd0f483e0111972165455).

Make sure to read the article and code comments, since things are a bit mismatchy right now if also customizing other plugins' template files, such as Event Tickets'.

We'd also love your 5-star rating on WordPress.org! 🥇

@mike-weiner

This comment has been minimized.

Copy link

@mike-weiner mike-weiner commented Feb 8, 2020

👍🏻 Thank you for the great explanations!

I have tweaked some of this code to be used within my Wordpress plugin. Everything is opened sourced on Github and I have given you and TEC credit for the code. If you know of anyone who wants to add an event's venue name and address to the tooltip on the full-month calendar view, this could be an option.

@cliffordp

This comment has been minimized.

Copy link
Owner Author

@cliffordp cliffordp commented Feb 10, 2020

Thanks for the thanks and for sharing how you used it! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment