[wp-hackers] $wp_rewrite->flush_rules() on plugin activation

Matt Jacob matt at mattjacob.com
Tue Dec 8 22:39:50 UTC 2009


Currently, in my callback that gets attached to
register_activation_hook(), I'm flushing the rewrite rules via the
init hook:

    add_action('init', array($this, 'FlushRewriteRules'));

My FlushRewriteRules() function is 2 lines:

    global $wp_rewrite;
    $wp_rewrite->flush_rules();

In the function that runs every time my plugin is loaded, I'm
filtering the rewrite_rules_array with this:

    add_filter('rewrite_rules_array', array($this, 'InsertRewriteRules'));

And my InsertRewriteRules() function just returns a union of the new
rules and the existing rules. Pretty simple, right?

My question, then, is whether I'm doing it wrong. When my plugin is
activated, do I need to *explicitly* filter rewrite_rules_array and
*explicitly* call flush_rules() instead of hooking into init? i.e.,
should my callback for register_activation_hook actually do the work
itself?

Anecdotal evidence seems to suggest that I'm doing it wrong, because
some people have to manually go to Settings > Permalinks > Save
Changes in order for my rewrite rules to take effect. I didn't
encounter that during my testing, but that's what users are reporting.

Thanks for any insights,

Matt


More information about the wp-hackers mailing list