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

Tuna Can tunamaxx at yahoo.com
Fri Nov 13 03:38:45 UTC 2009


Honestly, I would be ecstatic to be proven wrong on this. Do you have any idea why using a plugin with only the following code (plus the required plugin header of course):

function my_flush_rewrites() {
	global $wp_rewrite;
	$wp_rewrite->flush_rules();
}

function my_add_rewrites() {
	global $wp_rewrite;
	$my_new_non_wp_rules = array(
		'this/([0-9]+)$' => 'that/index.php?a=$1',
	);
	$wp_rewrite->non_wp_rules = $my_new_non_wp_rules + $wp_rewrite->non_wp_rules;
}

add_action( 'generate_rewrite_rules', 'my_add_rewrites' );
add_action( 'admin_init', 'my_flush_rewrites' );

...will automatically write the following in .htaccess:

RewriteRule ^this/([0-9]+)$ /that/index.php?a=$1 [QSA,L]

...but, if I set the .htaccess back to the WP default, then change the 'admin_init' to just 'init' like so:

add_action( 'init', 'my_flush_rewrites' );

...the changes aren't written to .htaccess unless I go to Settings / Permalinks and "Save Changes" manually?

HOWEVER, while I have had a little light at the end of the tunnel with this issue, my real question is whether it is possible to to flush_rules() via a THEME OPTIONS PAGE form submission. Since even discovering the 'admin_init' thing earlier today, I can't cause the changes to be made to .htaccess via my theme options page.

Any guidance would be GREATLY appreciated.

Thanks you,

Tony



--- On Thu, 11/12/09, Matt Jacob <matt at mattjacob.com> wrote:

> From: Matt Jacob <matt at mattjacob.com>
> Subject: Re: [wp-hackers] Best practice way for a plugin to use WP_Rewrite
> To: wp-hackers at lists.automattic.com
> Received: Thursday, November 12, 2009, 4:25 PM
> On Thu, Nov 12, 2009 at 12:23 PM,
> Tuna Can <tunamaxx at yahoo.com>
> wrote:
> > A little perseverance paid off...  My whole issue
> relates to trying to hook in the flush_rules() at 'init'
> instead of 'admin-init' as required.
> 
> I don't think that's entirely true. On plugin activation,
> I'm doing this:
> 
> add_action('init', array($this, 'FlushRewriteRules'));
> 
> Every time the plugin is loaded, I'm doing this:
> 
> add_filter('rewrite_rules_array', array($this,
> 'InsertRewriteRules'));
> 
> Seems to work pretty well for me. Maybe you were running
> into a
> problem somewhere else?
> 
> Matt
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
> 


      __________________________________________________________________
Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail.  Click on Options in Mail and switch to New Mail today or register for free at http://mail.yahoo..ca



More information about the wp-hackers mailing list