Opened 12 years ago
Last modified 5 years ago
#21682 reviewing defect (bug)
Rewrite endpoints are lost if a custom category or tag base is defined
Reported by: | sanchothefat | Owned by: | DrewAPicture |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 3.4 |
Component: | Rewrite Rules | Keywords: | dev-feedback has-patch needs-refresh |
Focuses: | Cc: |
Description
Problem
So this little bug was winding me up for a while.
The standard approach according to the codex for adding rewrite endpoints is to call the add_rewrite_endpoint()
function within the init hook. So far so good.
The problem occurs whenever WP_Rewrite::init()
is called after the init hook. It resets the endpoints array and so when rewrite rules are subsequently generated through the options-permalink.php admin page the rewrite rules are unknown to the system and hence don't work.
WP_Rewrite::init()
is called within WP_Rewrite::set_category_base()
, WP_Rewrite::set_tag_base()
and WP_Rewrite::set_permalink_structure()
.
In the latter it is only called if the permalink structure has changed so on first save of a change endpoints are lost. In the other 2 it is called every time if the slug doesn't match the default so rewrites are always lost.
Solutions:
- add an action hook to the start of
WP_Rewrite::rewrite_rules()
where endpoints should be added - store the endpoints at the start of
WP_Rewrite::init()
and restore them at the end - don't reset them at all
I think solution 3 would make sense, the endpoints could be defaulted to an empty array and I can't see any reason to want to reset them anyway.
I've attached a simple patch that works (for me at least).
NB. this problem may also affect the $extra_rules
and $non_wp_rules
but I haven't tested that theory yet.
Attachments (4)
Change History (16)
#4
@
12 years ago
- Cc scott@… added
This one just caught me out and cost me two hours. Looking forward to seeing this patch added to core soon.
#5
@
11 years ago
- Milestone changed from 3.6 to Future Release
I think this is likely dealt with in an indirect manner in [24060].
#9
@
9 years ago
- Keywords needs-refresh added
Patch needs to be regenerated. @wonderboymusic still want to pursue this for 4.4?
#12
@
9 years ago
21682.3.diff fixes the issues with the oldSlugRedirect.php
tests by making sure items in the $endpoints
array are unique fixing the issues with the endpoint slug being appended to generated urls multiple times.
The test that failed in addRewriteEndpoint.php
looks like it was relying on this bug to pass. The previously added endpoint of foo add_rewrite_endpoint( 'foo', EP_ALL, true );
being higher up in the generated rewrite rules array causing it to match when the endpoint in the test was http://example.org/page_endpoint/foo/
like it would with a normal url. Changing the test url to http://example.org/page_endpoint/baz/
allows the test to pass without other endpoints interfering.
Patch for rewrite endpoints being wiped in certain situations