#19275 closed defect (bug) (fixed)
Rewrite endpoints don't work with taxonomies
| Reported by: | sillybean | Owned by: | nacin |
|---|---|---|---|
| Priority: | normal | Milestone: | 3.4 |
| Component: | Permalinks | Version: | 3.1 |
| Severity: | normal | Keywords: | has-patch |
| Cc: | Focuses: |
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)
Change History (16)
#2
@
15 years ago
- Summary Rewrite endpoints don't work with category archives; added query vars treated as category slugs → Rewrite endpoints don't work with taxonomies
#4
@
15 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.
![(please configure the [header_logo] section in trac.ini)](/chrome/site/your_project_logo.png)
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.