Home › Forums › Calendar Products › Events Calendar PRO › Override Templates Via Custom Plugin
- This topic has 10 replies, 3 voices, and was last updated 7 years, 6 months ago by robert.
-
AuthorPosts
-
October 4, 2016 at 2:48 am #1172155robertParticipant
Hey folks,
As you know, the the normal Tribe Events template loader searches the following locations in order, until a match is found:
1. your theme / template path / template name
2. your theme / template name
3. default path / template nameI want to alter this slightly by injecting a search for the template within our own custom plugin (step 3 below), before finally defaulting to the Tribe Events core templates directory:
1. your theme / template path / template name
2. your theme / template name
3. your plugin / tribe-events / template name
4. default path / template nameMy question is, can this done? And if so how?
PS. I’ve managed to do this with WooCommerce, as described here: https://www.skyverge.com/blog/override-woocommerce-template-file-within-a-plugin/
October 4, 2016 at 5:27 am #1172207NicoMemberHi there Elaine,
Thanks for getting in touch with us! Interesting question here π
This is surely possible, as we don’t have any existing snippet or knowledge base article for this I created a base example for you to explore / build on top of:
/* Tribe, base code for adding overrides via custom plugin */
function tribe_filter_template_paths ( $file, $template ) {$custom_file_path = ABSPATH . 'wp-content/plugins/tribe-overrides/' . $template;
// file doesn't exist in custom path, go with the default option
if ( !file_exists($custom_file_path) ) return $file;// file exists in custom path, let's use it
return $custom_file_path;
}add_filter( 'tribe_events_template', 'tribe_filter_template_paths', 10, 2 );
The code above will add an additional check for the file in the custom directory, and if it exists it will override the template path. This is a pretty basic example not taking in account priority (load from theme if override exists) or namespaces (folder names for templates). The class that handles this is Tribe__Events__Templates (the-events-calendar/src/Tribe/Templates.php) with the method getTemplateHierarchy (line 538 – from which you can copy additional logic).
Please let me know if you can continue from this point or still need help,
Best,
NicoOctober 4, 2016 at 8:29 am #1172287robertParticipantHey Nico,
Thanks for a speedy response.
I’ve moved my custom single-events.php to wp-content/plugins/tribe-overrides/single-events.php
Then I added the supplied code but I can’t seem to get it to work. When trying to debug the code, it definitely finds the single-events.php file. Unfortunately it doesn’t seem to change what template is loaded. Tried another template as well but that doesn’t do anything neither.
Please forgive my incompetence, I’m not sure what to do to get it to work.
Thanks for your help
October 5, 2016 at 11:56 am #1172888NicoMemberThanks for following up Elaine!
Not sure where you pasted the code I provided, but if you add it to your current theme (or child theme) it works as expected for me. Of course this might not make sense if you are building this as a plugin but the code should be the same. Can you try to add it there? Also let’s add some debug info:
/* Tribe, base code for adding overrides via custom plugin */
function tribe_filter_template_paths ( $file, $template ) {echo 'Trying to hook a custom template for ('. $template .')- ';
$custom_file_path = ABSPATH . 'wp-content/plugins/tribe-overrides/' . $template;// file doesn't exist in custom path, go with the default option
if ( !file_exists($custom_file_path) ) {
echo 'Custom template not found';
return $file;
}// file exists in custom path, let's use it
echo 'Custom template returned';
return $custom_file_path;
}add_filter( 'tribe_events_template', 'tribe_filter_template_paths', 10, 2 );
This is the output I’m getting when viewing a single event page β https://cloudup.com/c69n3xw9TKu. I’ve placed an override of the ‘single-event.php’ template in ‘wp-content/plugins/tribe-overrides/single-event.php’. I also added the ‘(override)’ string before the title to make sure it’s loading the template from the overrides folder.
Please try to reproduce this sample test in your site and let me know if it works for you,
Best,
NicoOctober 10, 2016 at 4:52 am #1174443robertParticipantI was adding the code to a plugin instead of the child theme. D’oh!
Thanks for your help
October 11, 2016 at 7:11 pm #1175197NicoMemberThanks for the heads-up Elaine π
I understand this is working for you now, right? Are we good to close this thread out?
Please let me know about it,
Best,
NicoOctober 12, 2016 at 2:30 am #1175292robertParticipantHey Nico,
So this works great for overriding templates from the events calendar plugin.
I’d like to do the same for the template found at plugins\event-tickets-plus\src\views\wootickets\tickets.php
add_filter( ‘tribe_events_template’, ‘tribe_filter_template_paths’, 100, 2 ) doesn’t seem to work for this plugin. I’ve searched high and low and can’t find a suitable filter. I tried tribe_events_tickets_template but when debugging it doesn’t seem to even try to override this template.
Is it possible to override this template in a similar way?
Many thanks
October 13, 2016 at 8:48 am #1175950NicoMemberThanks for following up Elaine (or is it Jack?)! Event tickets templates are filtered in a different way with ‘dynamic’ filter (filter generated on the file for each template).
To override the ‘wootickets/tickets/’ template you can use the snippet below:
function tribe_filter_tickets_template_path ( $file ) {echo 'Trying to hook a custom template for ( wootickets/tickets ) - ';
$custom_file_path = ABSPATH . 'wp-content/plugins/tribe-overrides/wootickets/tickets.php';// file doesn't exist in custom path, go with the default option
if ( !file_exists($custom_file_path) ) {
echo 'Custom template not found';
return $file;
}// file exists in custom path, let's use it
echo 'Custom template returned';
return $custom_file_path;
}
add_filter( 'tribe_events_tickets_template_wootickets/tickets.php', 'tribe_filter_tickets_template_path' );
Feel free to change the path where it’s looking for the override template to something else. Just tested it and it works as expected so I guess you won’t have any troubles with it π
Best,
NicoOctober 17, 2016 at 7:10 am #1177643robertParticipantHi Nico,
Yeh it’s Jack, Elaine is my boss.
Works great!
Thank you for your help with this issue.
I would not have ever been able to do this without you.
Many thanks,
JackOctober 17, 2016 at 12:23 pm #1177860NicoMemberStocked to hear Jack π Really glad to of service!
Iβll go ahead and close out this thread, but if you need help with anything else please donβt hesitate to create a new one and we will be happy to assist you.
Best,
Nico -
AuthorPosts
- The topic ‘Override Templates Via Custom Plugin’ is closed to new replies.