1

I'm using Milo's answer to add a Rewrite Endpoint as part of my plugin.

Is there a straight-forward way to remove a Rewrite Rule upon deactivation?

I found this snippet but I'd expect a "remove_rewrite_endpoint" to match add_rewrite_endpoint

Also, how do I know what to look for in the rewrite rules list when add_rewrite_endpoint was used to add the rule?

1 Answer 1

3

All you need is flush the rewrite rules on deactivation. For example, in the main plugin file:

register_deactivation_hook( __FILE__, function() {
    flush_rewrite_rules();
});
9
  • 1
    How does it know which rewrite rule(s) to remove?
    – Meglio
    Jul 29, 2015 at 4:34
  • Flushing removes all rewrite rules with no distinction. Then they are rebuilt again from scratch.
    – cybmeta
    Jul 30, 2015 at 15:53
  • Are they rebuilt on the fly bit by bit as needed, or are they rebuilt all at once as soon as needed?
    – Meglio
    Aug 1, 2015 at 4:56
  • I'm not fully sure but as far I understand, they are all rebuilt at once in next page load and stored in database until they are flushed again. This database operation is the reason why flushing rewrite rules must be used carefully, only one time if is needed, never on every page load. For example, in this answer is used only on plugin activation because the plugin affect rewrite rules and need to be flushed.
    – cybmeta
    Aug 1, 2015 at 10:56
  • 1
    Yes, on init action hook preferably. You may be interested in a question I made some time ago where I learn that: Rewrite rules go away.
    – cybmeta
    Aug 5, 2015 at 12:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.