[wp-trac] [WordPress Trac] #46652: Add "template_loaded" action after a template has been included as part of the template hierarchy

WordPress Trac noreply at wordpress.org
Thu Jul 4 15:41:26 UTC 2019


#46652: Add "template_loaded" action after a template has been included as part of
the template hierarchy
-------------------------------------------+------------------------------
 Reporter:  adamtomat315                   |       Owner:  (none)
     Type:  enhancement                    |      Status:  new
 Priority:  normal                         |   Milestone:  Awaiting Review
Component:  Bootstrap/Load                 |     Version:  5.1
 Severity:  normal                         |  Resolution:
 Keywords:  needs-patch reporter-feedback  |     Focuses:
-------------------------------------------+------------------------------

Comment (by adamtomat315):

 Sure thing. We have a MVC Framework built on top of WordPress called
 Lumberjack. As part of this, we add the functionality to allow classes in
 files like page.php, single.php etc - to act as controllers.

 For example, page.php would look like this:

 {{{#!php
 <?php

 namespace App;

 use Rareloop\Lumberjack\Page;
 use Timber\Timber;

 class PageController
 {
     public function handle()
     {
         $context = Timber::get_context();
         $page = new Page();

         $context['post'] = $page;
         $context['title'] = $page->title;
         $context['content'] = $page->content;

         return view('templates/generic-page.twig', $context);
     }
 }
 }}}

 In order to achieve this, the Lumberjack Framework has to currently hook
 into the "template_include" function in order to detect whether or not the
 "PageController" class exists.

 Our code is open-source so you can see the whole class here:
 https://github.com/Rareloop/lumberjack-
 core/blob/v4.2.0/src/Providers/WordPressControllersServiceProvider.php#L18

 In short, we have to manually include the template ourselves. We don't
 want to do this, but we have to in order to check to see if the the class
 exists before we do anything else. Due to some of the complexities of what
 we are doing, we cannot return a value from our 'template_include'
 function, which prevents any other filters after ours from running. The
 biggest issue we've had here is WooCommerce + Lumberjack, as our code is
 preventing WooCommerce's template_include filter to run.

 That being said, there are some work arounds for us but they are all
 hacky. In an ideal world we would just hook in after the template has been
 included and it doesn't matter so much if we don't return anything as the
 template will still always be included.

 Hope that makes some sense! Any questions let me know.

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/46652#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list