Make WordPress Core

Opened 11 years ago

Closed 2 weeks ago

#23049 closed enhancement (wontfix)

Template hierarchy for 404

Reported by: mark-k's profile mark-k Owned by: johnbillion's profile johnbillion
Milestone: Priority: normal
Severity: normal Version:
Component: Themes Keywords: has-patch dev-feedback
Focuses: template Cc:


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)

template.diff (2.2 KB) - added by justinbusa 8 years ago.
23049.diff (2.2 KB) - added by justinbusa 7 years ago.
23049.2.diff (3.7 KB) - added by johnbillion 7 years ago.

Download all attachments as: .zip

Change History (20)

#1 @SergeyBiryukov
11 years ago

  • Component changed from Themes to Template

#2 @nacin
10 years ago

  • Component changed from Template to Themes
  • Focuses template added

#3 @SergeyBiryukov
10 years ago

  • Summary changed from Template hirarchy for 404 to Template hierarchy for 404

#4 follow-up: @obenland
10 years ago

Would you be up for writing a patch?

#5 in reply to: ↑ 4 @mark-k
10 years ago

Replying to obenland:

Would you be up for writing a patch?

I surely can try,

#6 @chriscct7
8 years ago

  • Keywords needs-patch added

#7 @justinbusa
8 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...

    [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...

    [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!

8 years ago

#8 @justinbusa
8 years ago

  • Keywords has-patch dev-feedback added; needs-patch removed

#10 @justinbusa
7 years ago

Hey @obenland! Any thoughts on this one?

#11 @johnbillion
7 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:

#12 @justinbusa
7 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?

7 years ago

#13 @johnbillion
7 years ago

  • Milestone changed from Awaiting Review to 4.9

7 years ago

#14 @johnbillion
7 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.

#15 @johnbillion
7 years ago

#41626 was marked as a duplicate.

This ticket was mentioned in Slack in #core by jeffpaul. View the logs.

7 years ago

#17 @johnbillion
7 years ago

  • Milestone changed from 4.9 to Future Release
  • Status changed from reviewing to accepted

Needs a bit more work and investigation into the test suite bug mentioned above.

#18 @johnbillion
2 weeks ago

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from accepted to closed

As much as I would love to get this in, I can't see it happening.

Note: See TracTickets for help on using tickets.