Opened 3 years ago
Last modified 6 weeks ago
#14310 reopened enhancement
Make template hierarchy filterable
| Reported by: |
|
Owned by: | |
|---|---|---|---|
| Priority: | normal | Milestone: | Future Release |
| Component: | Themes | Version: | |
| Severity: | normal | Keywords: | has-patch dev-feedback |
| Cc: | ocean90, 24-7@…, prodevstudio+wordpress@…, axwax, johnbillion@…, gruvii, alex.ciobica@…, navjotjsingh@…, eddie.moya+wptrac@…, xoodrew@…, frank@…, divinethemes, admin@…, ian_dunn@…, raulillana, lol@…, retlehs, wordpress@… |
Description (last modified by scribu)
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.
Attachments (6)
Change History (51)
- Summary changed from Template hierarchy filter to Make template hierarchy filterable
comment:4
F J Kaiser — 3 years ago
- Cc 24-7@… added
- Description modified (diff)
template_hierarchy.diff just renames the filter *_template_hierarchy, which seems more appropriate.
- Resolution set to fixed
- Status changed from new to closed
comment:10
markjaquith — 2 years ago
Nacin and Koopersmith expressed concerns about this. Not necessarily about the idea, but that it didn't get any peer review or iteration. Sort of landed under the radar. get_template_part() went through a lot of revisions, for example. I'd like to give this a chance for more discussion and development before it lands in core.
comment:11
markjaquith — 2 years ago
comment:12
markjaquith — 2 years ago
- Milestone changed from 3.1 to Future Release
- Resolution fixed deleted
- Status changed from closed to reopened
comment:13
ryan — 2 years ago
[17214] broke custom post type templates on wp.com.
comment:14
coffee2code — 2 years ago
[15611] has been in trunk for four months, which is quite a while. Pulling it during/after RC 2 stage is rather last minute, particularly since it's being reverted more out of principle than due to bugs or lack of merit.
comment:15
automattor — 2 years ago
comment:16
markjaquith — 2 years ago
- Milestone changed from Future Release to 3.1
- Resolution set to fixed
- Status changed from reopened to closed
I must have blindly hit "tc" or "mc" in that original revert attempt. It doesn't revert cleanly.
And on second thought, lack of review or not, coffee2code and ryan made a good point: it's been in trunk for a while. Taking it out has already caused problems for people running RC code who wanted to make use of the new filters. Leaving it in. Final answer, Regis.
Sorry for the disturbance on this one. Mea culpa.
comment:17
nacin — 2 years ago
- Resolution fixed deleted
- Status changed from closed to reopened
comment:18
nacin — 2 years ago
Per discussion with Mark and westi, while a full revert is difficult due to code churn, this filter should never have been added to core. Re-opening for consideration.
comment:19
westi — 2 years ago
I'm not convinced about the suitability of the new hook that was added here and I don't think that this has had enough discussion for us to want to keep this and preserve backwards compatibility in the future.
I would rather that we review the need for this new hook in future and discuss it more openly rather than sneak it in as part of a re-organisation of this code.
comment:20
westi — 2 years ago
comment:21
westi — 2 years ago
- Milestone 3.1 deleted
- Resolution set to fixed
- Status changed from reopened to closed
We remembered this again whilest discussing #12877 and if we are going to try and give access to the template hierarchy then we need to have a big discussion as to why we want to change these.
comment:22
nacin — 2 years ago
- Milestone set to 3.1
comment:23
scribu — 2 years ago
Fine with me.
comment:24
scribu — 2 years ago
- Milestone changed from 3.1 to Future Release
- Resolution fixed deleted
- Status changed from closed to reopened
Duplicate: #16994
comment:25
scribu — 2 years ago
Re-opened because the original goal of this ticket wasn't achieved.
comment:26
johnbillion — 2 years ago
- Cc johnbillion@… added
comment:27
follow-up:
↓ 28
scribu — 2 years ago
- Keywords dev-feedback added
Could someone explain to me again what's wrong with the "{$type}_template_hierarchy" filter?
Do we plan to drop the hierarchy approach altogether?
Otherwise, I don't see what the big deal is, considering we already have a "{$type}_template" filter on the next line.
comment:28
in reply to:
↑ 27
johnbillion — 2 years ago
Replying to scribu:
Could someone explain to me again what's wrong with the "{$type}_template_hierarchy" filter?
Anyone? I'd really love to get a filter added to the template hierarchy. Oh, the things I could do with it!
comment:29
gruvii — 2 years ago
- Cc gruvii added
comment:30
scribu — 2 years ago
A similar suggestion: #17788
comment:31
scribu — 23 months ago
Related: #13239.
comment:32
ciobi — 20 months ago
- Cc alex.ciobica@… added
comment:33
navjotjsingh — 19 months ago
- Cc navjotjsingh@… added
comment:34
eddiemoya — 17 months ago
- Cc eddie.moya+wptrac@… added
comment:35
ocean90 — 16 months ago
- Cc ocean90 added
Would be useful to extend the single hierarchy, see #18859.
comment:36
DrewAPicture — 16 months ago
- Cc xoodrew@… added
comment:37
Bueltge — 16 months ago
- Cc frank@… added
comment:38
divinethemes — 16 months ago
- Cc divinethemes added
comment:39
bainternet — 13 months ago
- Cc admin@… added
comment:40
iandunn — 9 months ago
- Cc ian_dunn@… added
comment:41
raulillana — 9 months ago
- Cc raulillana added
comment:42
sc0ttkclark — 4 months ago
- Cc lol@… added
comment:43
retlehs — 4 months ago
- Cc retlehs added
comment:44
louisremi — 2 months ago
I'm trying to recreate the template hierarchy logic on the client side with AngularJS. I want client-side templates to be picked by the Angular router based on current path and naming convention using the same rules that are used by WordPress on the server side.
The easiest way to achieve this is to get the computed array of $templates from the server side whenever the location path changes on the client side, then check which client-side template fits.
This implies that a generic filter inside get_category_template can provide the $templates array corresponding to any $type.
Unfortunately I see no alternative to this filter, and if it doesn't make it in 3.6, I will have to write a plugin that overwrites the content of wp-includes/template.php. I know this is a bad practice but there is simply no other way that I can think of.
comment:45
emzo — 6 weeks ago
- Cc wordpress@… added

template_candidates.diff makes all get_*_template() functions call get_query_template(), which consistently applies the appropriate filters.