[wp-trac] [WordPress Trac] #14310: Make template hierarchy filterable

WordPress Trac wp-trac at lists.automattic.com
Thu Jul 15 16:19:51 UTC 2010


#14310: Make template hierarchy filterable
-------------------------+--------------------------------------------------
 Reporter:  scribu       |       Owner:           
     Type:  enhancement  |      Status:  new      
 Priority:  normal       |   Milestone:  3.1      
Component:  Themes       |     Version:           
 Severity:  normal       |    Keywords:  has-patch
-------------------------+--------------------------------------------------

Old description:

> Currently, we have filters for each template type: home_template,
> author_template etc.
>
> The trouble is that these filters are applied on the final template path,
> after the template hierarchy has been traversed.
>
> It would be useful if there was another filter applied to the actual
> template hierarchy array, before it was sent to locate_template().
>
> == Example ==
>
> Take the author template hierarchy:
>
> author-{nicename}.php > author-{id}.php > author.php
>
> Say I want to add author-{role}.php before author.php.
>
> Sure, I could use the 'author_template' filter:
>
> {{{
> function author_role_template( $old_template ) {
>   // get current author's role
>
>   $new_template = locate_template( array( "author-$role.php" ) );
>
>   if( $new_template && 'author.php' == $old_template )
>     return $new_template;
>
>   return $old_template;
> }
> add_filter('author_template', 'author_role_template');
> }}}
>
> With an 'author_template_candidates' hook, I could manipulate the actual
> hierarchy:
>
> {{{
> function author_role_template( $templates ) {
>   // get current author's role
>
>   $new_template = array( "author-$role.php" );
>
>   $templates = array_merge(
>     array_slice( $templates, 0, -1 ), // before
>     $new_template,                    // inserted
>     array_slice( $templates, -1 )     // after
>   );
>
>   return $templates;
> }
> add_filter('author_template_candidates', 'author_role_template');
> }}}
>
> This would allow me to remove author-{id}.php if I wanted, etc.

New description:

 Currently, we have filters for each template type: home_template,
 author_template etc.

 The trouble is that these filters are applied on the final template path,
 after the template hierarchy has been traversed.

 It would be useful if there was another filter applied to the actual
 template hierarchy array, before it was sent to locate_template().

 == Example ==

 Take the author template hierarchy:

 author-{nicename}.php > author-{id}.php > author.php

 Say I want to add author-{role}.php before author.php.

 Sure, I could use the 'author_template' filter:

 {{{
 function author_role_template( $old_template ) {
   // get current author's role

   $new_template = locate_template( array( "author-$role.php" ) );

   if( $new_template && 'author.php' == $old_template )
     return $new_template;

   return $old_template;
 }
 add_filter('author_template', 'author_role_template');
 }}}

 With an 'author_template_candidates' hook, I could manipulate the actual
 hierarchy:

 {{{
 function author_role_template( $templates ) {
   // get current author's role

   $new_template = array( "author-$role.php" );

   $templates = array_merge(
     array_slice( $templates, 0, -1 ), // before
     $new_template,                    // inserted
     array_slice( $templates, -1 )     // after
   );

   return $templates;
 }
 add_filter('author_template_hierarchy', 'author_role_template');
 }}}

 This would allow me to remove author-{id}.php if I wanted, etc.

--

Comment(by scribu):

 template_hierarchy.diff just renames the filter *_template_hierarchy,
 which seems more appropriate.

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


More information about the wp-trac mailing list