[wp-trac] [WordPress Trac] #13239: Filter locate_template template_names variable

WordPress Trac wp-trac at lists.automattic.com
Tue Sep 7 18:41:36 UTC 2010

#13239: Filter locate_template template_names variable
 Reporter:  chrisbliss18  |       Owner:                 
     Type:  enhancement   |      Status:  new            
 Priority:  normal        |   Milestone:  Awaiting Review
Component:  Themes        |     Version:  3.0            
 Severity:  normal        |    Keywords:  has-patch      

Comment(by coffee2code):

 I intended to write a ticket very similar to this one, so I'm glad to see
 it's already here.

 There are at least three benefits to having such hooks:

  * Ability to alias a template file with another :
   * Statically (make requests for '404.php' use 'error.php')
   * Dynamically (i.e. if an admin is requesting 'searchform.php' serve
 them 'advanced-searchform.php')
  * Ability to hide a template : having `locate_template()` return "" would
 effectively make it seem as though the template doesn't exist
  * Ability to skip use of a template or certain type of template (i.e.
 prevent WP from searching for page-*, single-*, etc if you know you'll
 never use them or never want them used)

 The attached patch, 13239.c2c.diff:

  * Refreshes the original patch
  * Adds a third filter, one that operates before the function returns,
 sending the template ultimately located
  * Passes `$load` and `$require_once` along to all filters to provide
 additional context
  * Removes extra `file_exists()` call

 @nacin: I agree with your concern about the `file_exists()` call that the
 original patch is adding to every template searched for by
 `locate_template()`.  My patch omits that part, so there is no overhead
 being added, other than calling some additional filters, which we're
 generally cool with.

 My suggestion for @chrisbliss18, if my patch is accepted, would be to hook
 "locate_template-$template_name" and return "", which will cause the
 foreach to continue.  Then separately hook "locate_template_located" and
 see if `$template_names` contains the template you want to override (the
 same template you hooked via "locate_template-$template_name").  If so,
 you can perform your own `file_exists()`and what have you and return your
 desired target template.  You should be able to accomplish this without
 firing any unnecessary `file_exists()`.

 (Just as a related note: I ran across the issue while working on my
 Disable Search plugin.  `get_search_form()`, the recommended and widely
 used method for themes to display the site search form, calls:
 `locate_template(array('searchform.php'))` and if it's found, requires it
 and immediately returns.  Without one of the hooks in the attached patch,
 there is no way to suppress display of the search form without removing or
 renaming the template file.  One could argue that this is ticket-worthy
 itself, but it's an example of something that could be improved via this

 The hooks for `locate_template()` allow overriding of core template-
 location functionality.  Use of one of the filters could actually reduce
 the `file_exists()` checks if template(s) are being removed from being
 searched for since the hooks operate before any `file_exists()` are

 Some tangential relation to what scribu is doing #14310, but this applies
 closer to the metal and has more general applicability.

Ticket URL: <http://core.trac.wordpress.org/ticket/13239#comment:3>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software

More information about the wp-trac mailing list