WordPress.org

Make WordPress Core

Opened 12 months ago

Last modified 6 months ago

#21062 new enhancement

Add a 'template_file' hook to load_template()

Reported by: mikeschinkel Owned by:
Priority: normal Milestone: Awaiting Review
Component: Template Version: 3.4
Severity: normal Keywords: has-patch dev-feedback
Cc: info@…, mike@…, ryan@…, mikeschinkel@…, ian_dunn@…, DeanMarkTaylor

Description

Please consider adding a 'template_file' hook in load_template() to enable the capture of the template filename. I have built a panel for the Debug Bar to be able to show template files loaded but I need this 'template_file' hook to capture the template file names. This would be super useful for developers who are building sites with the complex template loading logic found in various theme frameworks et. al.

With the hook added the code for load_template() might look like this:

function load_template( $_template_file, $require_once = true ) {
  global $posts, $post, $wp_did_header, $wp_did_template_redirect, $wp_query, $wp_rewrite, $wpdb, $wp_version, $wp, $id, $comment, $user_ID;

  if ( is_array( $wp_query->query_vars ) )
    extract( $wp_query->query_vars, EXTR_SKIP );

  $_template_file = apply_filters( 'template_file', $_template_file, $require_once );

  if ( $require_once )
    require_once( $_template_file );
  else
    require( $_template_file );
}

Here's a screenshot showing the Theme Template Files panel I implemented so you can see the use-case. This plugin requires the hook I'm proposing in order to work and I have attached the plugin for other's review.

This hook could also allow the loading of the template file from other directories such as a shared directory on a server, as appropriate, but that's not the reason I found the need today.

The source for load_template() is found in /wp-includes/template.php.

Attachments (2)

add-template-file-hook.diff (457 bytes) - added by mikeschinkel 12 months ago.
Patch to add a 'template_file' hook to load_template()
debug-bar-theme-files-panel.zip (1.8 KB) - added by mikeschinkel 12 months ago.
Debug Bar Theme Files Panel plugin (requires proposed 'template_file' hook to work.)

Download all attachments as: .zip

Change History (13)

mikeschinkel12 months ago

Patch to add a 'template_file' hook to load_template()

mikeschinkel12 months ago

Debug Bar Theme Files Panel plugin (requires proposed 'template_file' hook to work.)

comment:1 toscho12 months ago

  • Cc info@… added

comment:2 mbijon12 months ago

  • Cc mike@… added

This would be a huge help to not only build with client-selected themes (that you may not know inside & out), but also to show clients how their chosen theme may be inefficient or non-performant.

++1

comment:3 scribu12 months ago

Similar proposal for locate_template(): #13239

comment:4 follow-up: scribu12 months ago

If the purpose is logging, a do_action() would suffice, no?

comment:5 ryanduff12 months ago

  • Cc ryan@… added

comment:6 in reply to: ↑ 4 mikeschinkel12 months ago

  • Cc mikeschinkel@… added

Replying to scribu:

If the purpose is logging, a do_action() would suffice, no?

Logging is one purpose, but as stated above I could easily see wanting to change the directory from which a template file is loaded so make it a filter since an action would have effectively the same overhead.

comment:7 mikeschinkel11 months ago

To whom it may concern: Is there any chance this could be considered for 3.5?

comment:8 follow-up: nacin11 months ago

I'm weary of adding hooks into load_template() and locate_template() without careful consideration, which are covered by a few other tickets. In this case, get_included_files() should be enough for debugging, yes?

comment:9 in reply to: ↑ 8 mikeschinkel11 months ago

Replying to nacin:

I'm weary of adding hooks intoload_template() and locate_template() without careful consideration

Weary? How can you be tired of adding hooks there, as there are no hooks there now!

Oh, you mean "wary"... ;-)

But seriously though, get_included_files() seems like a good solution, except the hook 'debug_bar_panels' is fired before /wp-includes/template-loader.php is included, so none of the theme files are available then.

Of course one can call get_included_files() and then use jQuery to add them back into the debug panel but personally I really much prefer to do things in PHP whenever possible; it's so much more testable and robust to use PHP.

That said, I'm curious what types of concerns you have able adding this hook, and hooks like @sival asks for in #18803? Aren't these little different than the 'template_include' hook which lets someone completely change the template?

comment:10 iandunn10 months ago

  • Cc ian_dunn@… added

comment:11 DeanMarkTaylor6 months ago

  • Cc DeanMarkTaylor added
Note: See TracTickets for help on using tickets.