[wp-hackers] Help with wp_rewrite?

DD32 wordpress at dd32.id.au
Sat Dec 1 02:22:14 GMT 2007


As well as Adding the filter, you need to force WP to actually make use of the filter, You'll need to force a refresh of the Rewrite rules.

eg:

//If the rewrite rules are regenerated, Add our pretty permalink stuff, redirect it to the correct queryvar
add_action('generate_rewrite_rules', 'test_add_rewrite_rules');
function test_add_rewrite_rules( $wp_rewrite ) {
	$new_rules = array( 
		'^shows.xml$' => 'wp-content/plugins/gigpress/feed.php',
		);

	$wp_rewrite->rules = $wp_rewrite->rules + $new_rules;
}

register_activation_hook(__FILE__,'test_flush_rules');
function test_flush_rules(){
	//Flush the rewrite rules so that the new rules from this plugin get added, 
	//This should only be done when the rewrite rules are changing, Ie. When this plugin is activated(Or Deactivated), For simplicity while developing using WP Rewrite, I flush the rules on every page load
	global $wp_rewrite;
	$wp_rewrite->flush_rules();
}

i'm not 100% sure if you can actually specify a file like this: 'wp-content/plugins/gigpress/feed.php',
WP Rewrite rules are NOT added to the .htaccess file unless you hook the .htaccess generation hook AFAIK, Instead, all rewrite rules are done internally by WP.

Instead, you'll need to use something like this:

add_action('generate_rewrite_rules', 'test_add_rewrite_rules');
function test_add_rewrite_rules( $wp_rewrite ) {
	$new_rules = array( 
		'^shows.xml$' => 'index.php?feed=gigpress',
		);

	$wp_rewrite->rules = $wp_rewrite->rules + $new_rules;
}

add_action('template_redirect','test_template');
function test_template($arg){
	global $wp_query;
	if( !isset($wp_query->query_vars['feed']) )
		return $arg;
	
	//Include gigpress feed code here...
	die(); //To stop anything futhur being run
}

I've been meaning to(For the last few months, Since the last question on this came up and i wrote all this example code) to write up a posting about using WP Rewrite, From memory there isnt too many resources available other than the codex, and thats highly limited.
For a example of how to use WP Rewrite, Have a look at this test plugin: http://dd32.id.au/files/wordpress/test-rewrite.php 

Keep in mind, That in the rewrite rules, if you specify 'index.php?feed=gigpress', then feed=gigpress IS NOT present in $_GET, WP never fills the superglobals when it does rewrite, instead you access it via the $wp_query object, wp_query also needs to be made aware that it should expect to recieve a parameter, That can be done via another filter which is shown in the plugin linked to, I've not included it in this code sniplets in this message as 'feed' is expected as WP uses it for the RSS feeds and whatnot.



On Sat, 01 Dec 2007 12:21:03 +1100, Derek Hogue <derek at amphibian.info> wrote:

> Hey all,
>
> I know this seems to be a common thread, but reading through old posts
> here and scouring the codex and forums is not bringing me to a solution.
>
> It's pretty simple - I'm looking for a way to add a simple Rewrite tot
> he WP Rewrite rules..  Following the codex example, and other
> examples, this seems like it should work:
>
> function gigpress_feed_rewrite($wp_rewrite) {
>      $feedrules = array('^shows.xml$' => 'wp-content/plugins/gigpress/
> feed.php');
>      $wp_rewrite->rules = $feedrules + $wp_rewrite->rules;
> }
>
> add_filter('generate_rewrite_rules', 'gigpress_feed_rewrite');
>
> But no go - I just get redirected back to my index.
>
> f I manually add this to my .htaccess file, above WP's conditions:
>
> RewriteRule ^shows.xml$ wp-content/plugins/gigpress/feed.php [L]
>
> It works like a champ.  And I am indeed updating my permalinks on my
> options page.
>
> Any ideas as to what I'm missing here?  Any help would be greatly
> appreciated - thanks.
>
> Derek
> --
> http://amphibian.info
>
>
>
> _______________________________________________
> 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