#42563 closed defect (bug) (invalid)
Flushing rewrite rules on plugin deactivation does nothing
Reported by: | brainfork | Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | 4.8.3 |
Component: | Rewrite Rules | Keywords: | |
Focuses: | Cc: |
Description
Attempting to flush custom rewrite rules added via plugin on same plugin deactivation as described in the codex/code reference using the following:
register_deactivation_hook( __FILE__, 'flush_rewrite_rules' );
Expected result:
Custom rewrite rules are removed when plugin is deactivated.
Actual result:
Custom rewrite rules persist, can only be cleared by manually triggering a flush
Change History (4)
#3
@
7 years ago
May I add that the unexpected behaviour directly conflicts with the example shown in the Plugin Handbook's Deactivation Hooks page
https://developer.wordpress.org/plugins/the-basics/activation-deactivation-hooks/
#4
@
7 years ago
- Milestone Awaiting Review deleted
- Resolution set to invalid
- Status changed from new to closed
Thanks for mentioning that @ptbello - I've fixed that entry to reference unregister_post_type()
.
The correct method here is to call unregister_post_type()
(and/or unregister_taxonomy()
) in the deactivation handler prior to calling flush_rewrite_rules()
.
For example:
function xxx_plugin_deactivate() { unregister_post_type( 'aaaaa' ); unregister_taxonomy( 'bbbbb' ); flush_rewrite_rules(); }
I'm closing this as invalid
as it's working as expected - if you've called register_post_type()
on that pageload, calling flush_rewrite_rules()
later in that page will deliberately include the post type rules as it doesn't have any way of detecting that it's happening during deactivation of a plugin that's registered a rule.
I suspect you are seeing this behaviour because you also have an action attached to 'init' that creates taxonomies or post types that result in new rewrite rules.
The problem is that the 'init' action is run before the deactivation hook when deactivating the (active) plugin, so your rewrite rules are still updated before being flushed.
I find you need to add something like the following check in your 'init' action before creating new taxonomies or post types:
It would be good if the Codex and other advice searchable on the Internet was updated with a description of this issue.