[wp-trac] [WordPress Trac] #19275: Rewrite endpoints don't work with category archives; added query vars treated as category slugs

WordPress Trac wp-trac at lists.automattic.com
Thu Nov 17 15:49:18 UTC 2011


#19275: Rewrite endpoints don't work with category archives; added query vars
treated as category slugs
--------------------------+-----------------------------
 Reporter:  sillybean     |      Owner:
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  Permalinks    |    Version:  3.2.1
 Severity:  normal        |   Keywords:
--------------------------+-----------------------------
 Adding a rewrite endpoint on category archives apparently confuses
 WP_Query and results in 404 errors. That is, according to all the
 documentation I have found on WP-Rewrite, this should work:

 http://example.com/category/slug/format/myformat/

 But instead, you have to use:

 http://example.com/category/slug/?format=myformat

 Excerpt from a sample plugin:

 {{{
 #!php
 // Add permalink structure, flush rules on activation/deactivation
 function uwc_ebook_activate() {
         uwc_ebook_add_rewrites();
         flush_rewrite_rules();
 }
 function uwc_ebook_deactivate() {
         flush_rewrite_rules();
 }
 register_activation_hook(__FILE__, 'uwc_ebook_activate');
 register_deactivation_hook(__FILE__, 'uwc_ebook_deactivate');

 // Add rewrite endpoint
 function uwc_ebook_add_rewrites() {
         add_rewrite_endpoint('format', EP_CATEGORIES);
         // this doesn't work either:
         // add_rewrite_endpoint('format', EP_ALL);
 }
 add_filter('init', 'uwc_ebook_add_rewrites');

 // Handle the endpoint display
 function uwc_ebook_template_redirect() {
 //      global $wp_query; var_dump($wp_query); exit;
         $format = get_query_var('format');
         $cat = get_query_var('cat');
         if ($format == 'epub' && isset($cat)) {
                 $category = get_category($cat);
                 if (!is_wp_error($category)) {
                         $uploads = wp_upload_dir();
                         $ebookurl =
 $uploads['baseurl'].'/ebooks/'.$category->slug.'/'.$category->slug.'.epub';
                         wp_redirect( $ebookurl, 301 );
                         exit;
                 }
         }
 }
 add_filter('template_redirect', 'uwc_ebook_template_redirect');

 // the rest of the plugin generates the epub file containing all posts in
 that category
 }}}

 Uncommenting the $wp_query line shows what's going on. Relevant parts of
 the var dump, from the Science & Technical subcategory of How-To:

 {{{
 #!php
 ["category_name"]=>
     string(4) "epub"
 ["query"]=>
   array(1) {
     ["category_name"]=>
     string(36) "how-to/science-technical/format/epub"
   }
 }}}

 Using /how-to/science-technical/?format=epub instead, WP correctly
 recognizes the category and format as two different query vars.

 I tested this in the latest nightly build, and it's broken there as well.

 Bill Erickson had a similar problem a few weeks ago, in which he used
 EP_ALL and found that it worked everywhere except category archives:
 https://gist.github.com/1287996

-- 
Ticket URL: <http://core.trac.wordpress.org/ticket/19275>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list