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

WordPress Trac wp-trac at lists.automattic.com
Mon Nov 1 18:47:48 UTC 2010


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

Comment(by coffee2code):

 After some consideration, I believe that the `locate_template` filter is
 the only absolutely necessary filter; two of the other three provide some
 specificity for hooking.  I believe everything can be done with just that
 one.  While the others may be nice to have, we'll still be fully served
 with just adding the `locate_template` filter.  My latest patch,
 13239.c2c.3.diff eliminates all but that filter.

 However, there is one thing that can't be done simply with the addition of
 that filter: allowing templates to be located outside of the current
 theme's (or its parent's) directory.  This is the functionality
 @chrisbliss18 was ultimately requesting.  The latest patch addresses this
 by checking to see if an absolute path was provided as part of the
 template name.  If so, then the template is checked first without the
 prepending of STYLESHEETPATH or TEMPLATEPATH.

 Here's a simple plugin (tested) using the filter to allow a plugin to
 override theme templates with templates contained in the plugin's
 templates/ subdirectory:

 {{{
 /*
 Plugin Name: Plugin that can override theme templates
 Version: 0.1
 Author: Mr. WordPress
 Description: Templates in this plugin's templates subdirectory will
 override the same-named template from the theme.
 */

 if ( !function_exists( 'add_plugin_template_path' ) ) :
 /**
  * The plugin's templates/ subdirectory may contain overrides for any
 theme template file
  */
 function add_plugin_template_path( $template_names ) {
         $new_template_names = array();
         foreach ( (array) $template_names as $tn ) {
                 $new_template_names[] = dirname( __FILE__ ) .
 '/templates/' . $tn;
                 $new_template_names[] = $tn;
         }
         return $new_template_names;
 }
 endif;
 add_filter( 'locate_template', 'add_plugin_template_path' );
 }}}

 The case for the other filters can be made at a later date if they end up
 being desired.

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


More information about the wp-trac mailing list