Override Templates Via Custom Plugin

Home Forums Calendar Products Events Calendar PRO Override Templates Via Custom Plugin

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1172155
    robert
    Participant

    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 name

    I 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 name

    My 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/

    #1172207
    Nico
    Member

    Hi 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,
    Nico

    #1172287
    robert
    Participant

    Hey 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

    #1172888
    Nico
    Member

    Thanks 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,
    Nico

    #1174443
    robert
    Participant

    I was adding the code to a plugin instead of the child theme. D’oh!

    Thanks for your help

    #1175197
    Nico
    Member

    Thanks 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,
    Nico

    #1175292
    robert
    Participant

    Hey 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

    #1175950
    Nico
    Member

    Thanks 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,
    Nico

    #1177643
    robert
    Participant

    Hi 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,
    Jack

    #1177860
    Nico
    Member

    Stocked 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

Viewing 10 posts - 1 through 10 (of 10 total)
  • The topic ‘Override Templates Via Custom Plugin’ is closed to new replies.