The flush_rewrite_rules
function is reliable in some contexts like a theme or a plugin based on hooks but I’m not sure if it works for a mu-plugin
My statement is based on the fact that WordPress is initialized in this way:
- call the
wp-settings.php
file - call the
do_action( 'muplugins_loaded' );
hook, here your plugin is initialized - call
$GLOBALS['wp_rewrite'] = new WP_Rewrite();
here the methodflush_rules
is initialized and available from now on do_action( 'setup_theme' );
is called and I bet all my money that on this hook theflush_rewrite_rules
will work
Solution?
Personally, I find reliable the deletion of the rewrite_rules option.
delete_option('rewrite_rules');
or
update_option('rewrite_rules', '' );
Whenever WordPress will lack the rewrite_rules
it will build them back, this is also what the flush_rules
method does.
There are points in WordPress execution flow where functions like this aren’t available. even in the core of WordPress I found this statement
// Rewrite rules can't be flushed during switch to blog.
delete_option( 'rewrite_rules' );
The only problem would be the performance, don’t do this on every request because it is a hard process to build them back.
As I can see you want to flush them only at the first call and this is a good thing.
P.S: I’m not such a self-promo fan but I’ve also written an article about this long time ago and I think it still stands up for this