add_rewrite_rule, plugin activation and plugin deactivation

The problem with calling flush_rewrite_rules() is that the rules instantly get regenerated, while your plugin’s hooks are still active.

What I usually do, because it’s the simplest route to success, is delete the rewrite_rules option on activation/deactivation. The rules will then get regenerated on the next pageload instead of the current one:

register_deactivation_hook( __FILE__, 'my_plugin_deactivation');
function my_plugin_deactivation() {
   delete_option( 'rewrite_rules' );
}

A more ideal solution is to remove your rules from the stack before flushing, but that’s kind of a pain to do (see https://core.trac.wordpress.org/ticket/29118).

Leave a Comment