WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am starting to run into situations on projects where I am combining multiple plugins which all use add_rewrite_rule and flush_rewrite_rules at different points. As we all know, calling flush_rewrite_rules on every page load is a bad thing.

The problem arises when Plugin A has not yet added it's rewrite rules, but Plugin B calls flush_rewrite_rules. Plugin A is a good citizen, and doesn't call flush_rewrite_rules unless it needs to and so now it's rewrite rules are no longer present.

I've been looking for an action which gets called before the rules are collated by flush_rewrite_rules, which I can hook for my add_rewrite_rule calls to be sure all rules are present before they are compiled, but I've not found one. Any suggestions?

share|improve this question
Raised the possibility of an additional hook in WP trac: core.trac.wordpress.org/ticket/18450 – Simon Wheatley Aug 17 '11 at 8:18

The easiest solution to this is to hook your add calls on to the last action which gets fired before the rules are rebuilt during the flush process.

The first part of the flushing process is to delete the rewrite_rules option. When this happening the following actions will fire:

do_action( 'delete_option', $option );
do_action( "delete_option_$option", $option );
do_action( 'deleted_option', $option );

Note: If there is no rewrite_rules option in the db then these actions are not fired so you will also need to ensure to register your rules when someone calls get_option('rewrite_rules') and it is going to return an empty string - there are similar filters in get_option you can use for this.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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