Make WordPress Core

Opened 13 years ago

Closed 13 years ago

Last modified 13 years ago

#19275 closed defect (bug) (fixed)

Rewrite endpoints don't work with taxonomies

Reported by: sillybean's profile sillybean Owned by: nacin's profile nacin
Milestone: 3.4 Priority: normal
Severity: normal Version: 3.1
Component: Permalinks Keywords: has-patch
Focuses: Cc:

Description

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:

// 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:

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

Attachments (1)

19275.diff (4.7 KB) - added by nacin 13 years ago.

Download all attachments as: .zip

Change History (16)

#1 @billerickson
13 years ago

  • Cc bill.erickson@… added

#2 @nacin
13 years ago

  • Summary changed from Rewrite endpoints don't work with category archives; added query vars treated as category slugs to Rewrite endpoints don't work with taxonomies

The issue is a change in 3.1, which made taxonomies all be handled like any other permastruct. This means the endpoints are no longer properly checked. Surprised it was missed until now.

Unlike register_post_type(), register_taxonomy() does not provide a way to specify an endpoint. So fixing this also requires an API enhancement.

#3 @nacin
13 years ago

  • Version changed from 3.2.1 to 3.1

@nacin
13 years ago

#4 @nacin
13 years ago

19275.diff standardizes on ep_mask being an optional argument for $args['rewrite'] for either taxonomies or post types, and makes endpoints work for categories and tags.

Last edited 13 years ago by nacin (previous) (diff)

#5 @nacin
13 years ago

  • Keywords has-patch added

#6 @sillybean
13 years ago

Patch works perfectly. Thanks!

#7 @mintindeed
13 years ago

  • Cc gabriel.koen@… added

#8 @nacin
13 years ago

  • Milestone changed from Awaiting Review to 3.4

#9 @nacin
13 years ago

  • Owner set to nacin
  • Resolution set to fixed
  • Status changed from new to closed

In [19738]:

Add 'ep_mask' as an argument to the 'rewrite' array for register_post_type() and register_taxonomy(). Keeps 'permalink_epmask' compatible as an argument for post type registrations. Fixes endpoints for category and tag pages. fixes #19275.

#10 @nacin
13 years ago

In [19740]:

Args is an object, not an array. props duck_, aaroncampbell. see #19275.

#11 @nacin
13 years ago

For the extra stuff in [19738], see [19739].

#12 @nacin
13 years ago

In [19741]:

Clean up and better document create_initial_taxonomies(). see #19275.

#13 @husobj
13 years ago

  • Cc ben@… added

#14 @dempsey
13 years ago

  • Cc dempsey@… added

#15 @markoheijnen
13 years ago

  • Cc marko@… added

Just curious how does this effect #12779 (better support for custom post types in WP_Rewrite).

Note: See TracTickets for help on using tickets.