Opened 10 years ago
Last modified 6 years ago
#23049 accepted enhancement
Template hierarchy for 404
Reported by: |
|
Owned by: |
|
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | |
Component: | Themes | Keywords: | has-patch dev-feedback |
Focuses: | template | Cc: |
Description
load 404-{post-type}.php when url structure matches post permalink structure but there is no post at that address.
load 404-{taxonomy}.php when url structure matches taxonomy permalink structure but doesn't match any specific taxonomy tag URL.
And so on....
The idea is to have different 404 pages based on the context to which the URL refers. For example if a site has a blog and a shop it might be better to show a blog specific 404 page when the URL might be interpreted as a post and a shop specific 404 page when the URL might be interpreted as a product.
Attachments (3)
Change History (19)
#7
@
7 years ago
Have a look at the attached patch and let me know what you think.
Since we can't use the is_*
checks on a 404 page and things like get_queried_object
won't return anything, this resorts to checking for the existence of query vars and loading the appropriate templates if a query var exists.
You'll see that each item in the $checks
array is a query var that is mapped to template slugs that will be used to build the 404 template filenames if that query var exists.
For example, if I visit a product that doesn't exist at http://localhost/wordpress/product/does-not-exist/
, the following template array will be built...
Array
(
[0] => 404-single-product.php
[1] => 404-single.php
[2] => 404-singular.php
[3] => 404.php
)
Similarly, if I visit a product category that doesn't exist, the following template array will be built...
Array
(
[0] => 404-taxonomy-product_cat.php
[1] => 404-taxonomy.php
[2] => 404-archive.php
[3] => 404.php
)
All in all, the following 404 templates are possible with this patch...
- 404-taxonomy-{$taxonomy}.php
- 404-taxonomy.php
- 404-archive.php
- 404-category.php
- 404-tag.php
- 404-author.php
- 404-date.php
- 404-attachment.php
- 404-single.php
- 404-singular.php
- 404-single-{$post_type}.php
- 404-page.php
In the patch, I tried to follow the order that templates are loaded in wp-includes/template-loader.php
and include as many of the templates that I could from there (e.g. a 404-single.php for parody with single.php).
Templates that didn't make sense or couldn't be included due to limitations are...
- embed
- search
- front page
- home
- post type archives
- paged
I'm not sure if using get_query_var
is the right approach, but given that the data we have available to determine the template on 404 pages is seemingly limited (let me know if I'm wrong there), this appears to be a good solution.
Let me know what you think!
#11
@
6 years ago
- Owner set to johnbillion
- Status changed from new to reviewing
I could get behind this. I can immediately see uses for 404-single-{$post_type}.php
.
The order here is important. It should match the order used in the template loader: https://core.trac.wordpress.org/browser/tags/4.7.4/src/wp-includes/template-loader.php?marks=46-65#L43
#12
@
6 years ago
Thanks, @johnbillion! I checked and it looks like it just needed a small tweak to be inline with the template loader but I could be misunderstanding you. Can you have a look at the new patch and let me know?
#14
@
6 years ago
23049.2.diff is a simplified 404 template hierarchy. More details to come once I hunt down a taxonomy rewrite rule bug in the test suite.
Would you be up for writing a patch?