[wp-hackers] 2 Questions: $_REQUEST equivalent and using GET in mod_rewrite

DD32 wordpress at dd32.id.au
Thu Aug 30 01:20:17 GMT 2007


On Thu, 30 Aug 2007 05:00:39 +1000, Alex Andrews <awgandrews at gmail.com>
wrote:

> On 29/08/2007, DD32 <wordpress at dd32.id.au> wrote:
>> > How can I get /artists/x/ to translate to artists.php?id=x or  
>> whatever.
>> By the sounds of it, you've developed the standalone app not quite how
>> you'd write a WP plugin.
>
> Now I am cooking on gas. Decided to make a swish template loader to
> intergate the previously existing script. But there is a further
> mod_rewrite problem.
>
> Here is the current function:
>
> add_action('generate_rewrite_rules', 'ribcage_add_rewrite_rules');
> function ribcage_add_rewrite_rules( $wp_rewrite ) {
> 	$new_rules = array(
> 		"(artists)/(.*)" => 'index.php?ribcage=1&artist_slug=' .
> $wp_rewrite->preg_index(2),
> 		"(artists)" => 'index.php?ribcage=1&artist_index=1'
> 	);
>
> 	$wp_rewrite->rules = $wp_rewrite->rules + $new_rules;
> }
>
> Now this obviously passes like this
> /artists/mrdave
> And that works fine. But how about if one wants to pass more than one
> variable. For example:
> /artists/mrdave/bio
> or
> artist/mrdave/myrecord
>
> where my record is the name of a record or whatever. Or whatever. I
> presume this has something to do with preg_index();? Perhaps.

You'd use something like this for that:
   	$new_rules = array(
   		"(artists)/(.*)/(.*)" => 'index.php?ribcage=1&artist_slug=' .
$wp_rewrite->preg_index(2) . '&artist_page=' . $wp_rewrite->preg_index(3),
   		"(artists)/(.*)" => 'index.php?ribcage=1&artist_slug=' .
$wp_rewrite->preg_index(2),
   		"(artists)" => 'index.php?ribcage=1&artist_index=1'
   	);

You'd then also add the artist_page query var:
add_filter('query_vars', 'test_queryvars' );
function test_queryvars( $qvars ){
	$qvars[] = 'artist_slug';
	$qvars[] = 'artist_page';
	return $qvars;
}

and then finally, access them:
die("Plugin for Artist: " . $wp_query->query_vars['artist_slug'] . " page:
" . $wp_query->query_vars['artist_page']);



With the rewrite rules you just look at it as a Regular Expression(Know
Regular expressions at all?),
in the regular expression: "(artists)/(.*)/(.*)" you suround matches with
the () brackets,
Then in the replacement rule: 'index.php?ribcage=1&artist_slug=' .
$wp_rewrite->preg_index(2) . '&artist_page=' . $wp_rewrite->preg_index(3),

You build a string, but use ->preg_index(n) for the match you want, Eg,
the first is ->preg_index(1), the 2nd set of brackets will be
->preg_index(2), the 3rd, ->preg_index(2), right up to the number of
matches you have in your rule.
-- 
Using Opera's revolutionary e-mail client: http://www.opera.com/mail/


More information about the wp-hackers mailing list