[wp-trac] [WordPress Trac] #11312: Add 'handle_404' hook to allow plugins to route URLs

WordPress Trac wp-trac at lists.automattic.com
Mon Jan 4 18:39:18 UTC 2010


#11312: Add 'handle_404' hook to allow plugins to route URLs
---------------------------+------------------------------------------------
 Reporter:  mikeschinkel   |       Owner:  ryan                    
     Type:  enhancement    |      Status:  new                     
 Priority:  normal         |   Milestone:  Future Release          
Component:  Rewrite Rules  |     Version:  2.9                     
 Severity:  normal         |    Keywords:  needs-patch dev-feedback
---------------------------+------------------------------------------------
Changes (by mikeschinkel):

 * cc: mikeschinkel@… (added)


Comment:

 For some reason I did not get notification of your responses.  I think I
 forgot to check CC.

 I'm working on another project and am ''yet again'' running into the
 problem this causes.

 @dd32 - Please review the process flow. The problem is in the handle_404()
 function. The only states where it does not throw a 404 are:

 * count($wp_query->posts) !=0
 * !is_search()
 * is_tag()
 * is_category()
 * is_author()

 Nowhere does it allow for a custom URL that should simply be routed. Yes I
 can use the template_redirect hook but it will issues a 404 that I can't
 control. If the problem isn't obvious now then please explain how someone
 can use template_redirect hook w/o having a 404 already issue w/o fixing
 handle_404()?

 Here's the dirty, ugly hack I've had to get around a 404:
 {{{
 static function the_posts($posts) {
         if (is_my_custom_url()) {
                 $posts[] = null; // Dirty, Ugly hack to get around
 WordPress throwing a 404 error.
         }
         return $posts;
 }
 add_filter('the_posts', array(__CLASS__, 'the_posts'));

 static function parse_query($request) {
         if (is_my_custom_url()) {
                 $wp_query->is_single = false;
                 $wp_query->is_home = false;
                 $wp_query->is_404 = false;
         }
 }
 add_filter('parse_query', array(__CLASS__, 'parse_query'));
 }}}

 @Otto42 If you read the code in WordPress for handle_404() you'll find
 that WordPress doesn't respect the 404 setting; even if you set it to
 false in a prior hook WordPress still thinks it's smarter than you and
 decides for itself if it should be 404 and when it does it sends the 404
 immediate to the browser.

 Basically if you want to use a custom URL where the URL isn't trying to
 resolve to something WordPress expects like a post or an archive or a
 search then it issues a 404, period.

 I've created a plugin that demonstrates the problem. The plug is called
 MyRouterTest and it routes a path of "mypath" using template_redirect.  It
 triggers a 404 and won't display on most browsers (but it will display on
 Mac/Safari, go figure):

 * http://wordpress.pastebin.com/f1f1fcc55

 AFAICK, there is no way to keep this from triggering a 404 with using some
 kind of ugly hack as I showed above.

 Or maybe I'm not seeing something obvious?  If so, please educate me. But
 if not please don't simply dismiss this without at least trying to
 understand the problem is.

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


More information about the wp-trac mailing list