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

Austin Matzko if.website at gmail.com
Tue Dec 8 22:49:43 UTC 2009

On Tue, Dec 8, 2009 at 4:39 PM, Matt Jacob <matt at mattjacob.com> wrote:
> 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?

Yes, you need just to call flush_rules() directly in the callback
you've attached to register_activation_hook.  The problem is that when
the plugin is activated and activate_[plugin name] is called, "init"
has already been called.  So attaching a callback to it at that point
will be fruitless.

More information about the wp-hackers mailing list