4

I see a lot of example showing to call add_rewrite_rule in your init hook, e.g.

function add_my_custom_rewrite()
{
   add_rewrite_rule(...
}

add_action('init', 'add_my_custom_rewrite');

and in your plugin activation hook,

register_activation_hook( __FILE__, 'my_plugin_activation');

function my_plugin_activation()
{
   add_my_custom_rewrite();
   flush_rewrite_rules(false);
}

So, when your plugin is activated, you rewrite will be flushed into db, seems working, but, there is a main problem

It still not solve the issue to remove the rewrite when people deactivate your plugin, you cannot simply place flush_rewrite_rules in your deactivation hook since init was already called before deactivate, and there is no way to remove existing rewrite rule., e.g.

register_deactivation_hook( __FILE__, 'my_plugin_deactivation');

function my_plugin_deactivation()
{
   flush_rewrite_rules(false); // will not work, since init (hence add_my_custom_rewrite) was already called at this point
}

Are there better way to solve the conflict between add_rewrite_rule, plugin activation and plugin deactivation?

2
  • 1
    You are wrong. Running flush_rewrite_rules() on deactivatioin hook removes the rewrite rule from database. As you said, the init hook is called before the deactivation hook, so flush_rewrite_rules() on the deactivation hook runs after and remove all rewrite rules. The rewrite rules are rebuild in the next page load when your plugin is not active.
    – cybmeta
    Nov 18, 2014 at 16:07
  • Hi, I've just found this: wordpress.stackexchange.com/questions/36152/…, sound like it is a bug in WP itself.
    – Howard
    Nov 18, 2014 at 17:46

1 Answer 1

2

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).

2
  • If you do that then you need to flush rewrite rules straight after, since the rules have been regenerated. However, when you do that your custom rules will come back, which means the above won't really work.
    – adamj
    Oct 1, 2015 at 23:57
  • With this method, WordPress will automatically regenerate the rules on the next (front-end) page load, when it finds that the option doesn't exist -- and when the plugin is no longer active. Oct 2, 2015 at 0:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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