[wp-hackers] Best practice way for a plugin to use WP_Rewrite

scribu scribu at gmail.com
Tue Nov 10 12:20:48 UTC 2009

A "permalink_structure_saved" hook sounds good.

Or even better would be if WP wouldn't regenerate the rules when the
user doesn't change anything.

On 11/10/09, Matt Jacob <matt at mattjacob.com> wrote:
> Hi everyone,
> I've searched long and hard for a definitive answer to this question,
> but I came across a lot of misinformation and bad advice along the
> way. Basically, all I want to do is add a few known (not dynamic) URIs
> to WordPress via a plugin---with pretty permalinks if they're enabled
> and standard query string variables if they're not.
> What I've got so far works perfectly fine, except in a couple edge
> cases. I've got this function that's called on plugin activation and
> when someone saves the plugin's options page:
>     private function DeployRewriteRules() {
>         global $wp_rewrite;
>         add_filter('rewrite_rules_array', array($this,
> 'InsertRewriteRules'));
>         $wp_rewrite->flush_rules();
>     }
>     public function InsertRewriteRules($rules) {
>         $new_rules = array(
>             'pingfm/post/([a-zA-Z0-9]{32})/?$' =>
> 'index.php?pa=post&pk=$matches[1]',
>             'pingfm/status/([0-9]+)/?$'        =>
> 'index.php?pa=status&pi=$matches[1]',
>             'pingfm/feed/?$'                   => 'index.php?pa=feed',
>         );
>         return $new_rules + $rules;
>     }
> Unfortunately, sometimes other things modify the rewrite rules, and
> then my users have to deactivate/reactivate or save the plugin
> settings page again in order to get mine back.
> As of 2.8, I know I can hook into the "permalink_structure_changed"
> action, and this gets me about 90% of the way there. The edge case I
> mentioned earlier is when someone hits the save button on the
> Permalink Settings page without actually changing anything. The new
> rules are saved out, but the action is skipped over.
> I *could* call my DeployRewriteRules() function on every page load,
> but I know that flushing the rewrite rules is pretty
> resource-intensive, and that's not acceptable to my users.
> And that leaves me with this: what is the best practice method for
> ensuring that my rewrite rules are present at every page load? Is
> there one, or are there just varying degrees of bad methods? Would it
> be feasible to add a "permalink_structure_saved" hook in a future
> release?
> Thanks!
> Matt
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers


More information about the wp-hackers mailing list