1

I am developing a plugin, and am using add_rewrite_rule followed by flush_rewrite_rules during plugin activation.

On plugin deactivation I am using just flush_rewrite_rules to remove the rule that I had added.

Now let's assume another plugin had also added a rewrite rule. Would calling flush_rewrite_rules not remove this other plugins rewrite rule as well, even though the other plugin is still active?

EDIT:

Some notes to remember:

  • flush_rules() does the same as flush_rewrite_rules(). See Milo's comments below his accepted answer.

  • There is an issue in bug tracker https://core.trac.wordpress.org/ticket/29118 that shows the best practice, but also shows the problem of having to use INIT to conitinually add your rule.

1 Answer 1

2

This is why you need to add rewrite rules on the init action on every request, as well as on plugin activation.

When your plugin deactivation hook is run, other plugin init hooks have already run, so their rules exist in the global variable that stores them for the life of each request.

Flushing rewrite rules empties the option that stores the rules long-term, and then repopulates it with whatever is in that global variable.

8
  • 1
    Just don't flush the rules on init too - will increase load much more than is needed!
    – Tim Malone
    Commented Jun 2, 2016 at 4:02
  • The WordPress Codex specifically recommends that you do NOT run flush_rewrite_rules on init codex.wordpress.org/Function_Reference/flush_rewrite_rules
    – jsherk
    Commented Jun 2, 2016 at 14:08
  • 1
    @jsherk You don't flush rules on init, but you must add them on init on every request, as well as any code that generates rules, like registering post types and taxonomies.
    – Milo
    Commented Jun 2, 2016 at 15:04
  • @Milo Okay now I see what you are saying. This makes more sense.
    – jsherk
    Commented Jun 2, 2016 at 19:07
  • Another thought/question ... how does flush_rules() compare to flush_rewrite_rules()? When/why would you use one versus the other?
    – jsherk
    Commented Jun 2, 2016 at 19:08

Your Answer

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

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