flush_rewrite_rules() cancels the effect of add_rewrite_rule()

Rewrite rules are stored in an option. When flush_rewrite_rules() is called, this option is removed, WordPress collects all rules registered during the current request and writes that option new into the database.

You registration doesn’t run on every request, so it doesn’t exist, when the option is rewritten.

Register the rule on ever page load, flush the rewrite rules on activation. Separate the registration and the flush.

register_activation_hook(__FILE__,'PluginA_activation');
add_action( 'wp_loaded', 'PluginA_rewrite_rules' );

function PluginA_activation()
{
    PluginA_rewrite_rules();
    flush_rewrite_rules();
}

function PluginA_rewrite_rules()
{
    add_rewrite_tag('%foo%','([^&]+)');
    add_rewrite_rule('foo/([^/]+)','index.php?foo=$matches[1]','top');  
}

Leave a Comment