As one of my plugin's features, I'm trying to define a custom url structure, like http://www.mysite.tld/foo/{action}
. I'm using add_rewrite_rule()
along with flush_rewrite_rules()
on plugin activation, which works as expected.
Until flush_rewrite_rules()
gets called again, during another request, either by hand or by enabling another plugin that does so. When this happens, my rewrite rule suddenly disappears (I'm using Rewrite Rules Inspector to check this).
The minimum code needed to reproduce the issue is this:
<?php
/*
Plugin Name: PluginA
*/
register_activation_hook(__FILE__,'PluginA_activation');
function PluginA_activation()
{
add_rewrite_tag('%foo%','([^&]+)');
add_rewrite_rule('foo/([^/]+)','index.php?foo=$matches[1]','top');
flush_rewrite_rules();
}
Any idea what I'm doing wrong?