WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 2 years ago

Last modified 2 years ago

#19275 closed defect (bug) (fixed)

Rewrite endpoints don't work with taxonomies

Reported by: sillybean Owned by: 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 2 years ago.

Download all attachments as: .zip

Change History (16)

comment:1 billerickson2 years ago

  • Cc bill.erickson@… added

comment:2 nacin2 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.

comment:3 nacin2 years ago

  • Version changed from 3.2.1 to 3.1

nacin2 years ago

comment:4 nacin2 years ago

19275.diff standardizes on ep_mask being an optional argument for $argsrewrite? for either taxonomies or post types, and makes endpoints work for categories and tags.

Version 0, edited 2 years ago by nacin (next)

comment:5 nacin2 years ago

  • Keywords has-patch added

comment:6 sillybean2 years ago

Patch works perfectly. Thanks!

comment:7 mintindeed2 years ago

  • Cc gabriel.koen@… added

comment:8 nacin2 years ago

  • Milestone changed from Awaiting Review to 3.4

comment:9 nacin2 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.

comment:10 nacin2 years ago

In [19740]:

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

comment:11 nacin2 years ago

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

comment:12 nacin2 years ago

In [19741]:

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

comment:13 husobj2 years ago

  • Cc ben@… added

comment:14 dempsey2 years ago

  • Cc dempsey@… added

comment:15 markoheijnen2 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.