[wp-trac] [WordPress Trac] #23049: Template hierarchy for 404

WordPress Trac noreply at wordpress.org
Wed Apr 20 06:57:15 UTC 2016


#23049: Template hierarchy for 404
-------------------------+------------------------------
 Reporter:  mark-k       |       Owner:
     Type:  enhancement  |      Status:  new
 Priority:  normal       |   Milestone:  Awaiting Review
Component:  Themes       |     Version:
 Severity:  normal       |  Resolution:
 Keywords:  needs-patch  |     Focuses:  template
-------------------------+------------------------------

Comment (by justinbusa):

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

 {{{#!php
 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...

 {{{#!php
 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!

--
Ticket URL: <https://core.trac.wordpress.org/ticket/23049#comment:7>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list