[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
Tue Mar 26 10:59:34 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 | Keywords:
Focuses: |
----------------------------+-----------------------------
As part of the template hierarchy, WordPress works out what template it
should use. It then passes this template through the `template_include`
filter, which allows you to change the template name.
This template is then included and that's the end of things. Here's the
current snippet showing the filter and the include (from here
https://core.trac.wordpress.org/browser/tags/5.1.1/src/wp-includes
/template-loader.php#L77)
{{{#!php
<?php
// ...
if ( $template = apply_filters( 'template_include', $template ) ) {
include( $template );
} elseif ( current_user_can( 'switch_themes' ) ) {
// ...
}}}
However, we need to run some code ''after'' the template has been
included. We can't rely on the next action to fire (which is `get_header`
I believe)
---
**I suggest that a new action is added after the template is included. For
example:
**
{{{#!php
<?php
// ...
if ( $template = apply_filters( 'template_include', $template ) ) {
include( $template );
// Add an action here
do_action('template_included', $template);
} elseif ( current_user_can( 'switch_themes' ) ) {
// ...
}}}
---
For some context, as to why we need this change... we have some code that
allows us to use PHP classes (controllers) in these template files. We
currently hook into "template_include", do the include ourselves then
prevent WP from doing the include by not returning anything. However we
are running into issues where other hooks on "template_include" not
behaving correctly now because we are not returning the template.
We can get around this for now by making sure our filter runs last by
setting a high priority. However it would be ideal for us to run our code
after the include has been done. Happy to provide more details or code if
needed.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/46652>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list