1

I need to create rewrite rules for my plugin. This plugin will be running on a very large WordPress Network (+2000 sites) so performance is important.

Most tutorials and the codex recommend to flush_rewrite_rule on plugin activation and then to add_rewrite_rule() on init. But since rewrite rules are stored in DB it seems to me that we are wasting resources running add_rewrite_rule on every init (=every page load).

On the other hand you can't only rely on the plugin activation hook because if another plugin flush the rules then your rewrite rules would be lost.

I know that add_rewrite_rule is just some simple string and array manipulation and is not SUPER expensive in term of resources but it still feel not right....

Am I missing something?

1 Answer 1

1

The add_rewrite_rule function doesn't change the database. Just an array in memory. It is not an expensive operation.

The alternative is to add code to detect whether your rule is there, which is actually more expensive than just modifying the array.

1
  • Thanks Otto, I'm taking over another developer on this plugin. I assumed add_rewrite_rule was writing to the DB because the plugin's rewrite rules are appearing in the "rewrite option" in the wp_options table. I wonder how my predecessor achieved that then...I'll look into it on Monday. Commented Jan 31, 2016 at 15:53

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.