[wp-hackers] Permalinkg Rule

Dion Hulse (dd32) wordpress at dd32.id.au
Mon Jun 7 10:03:23 UTC 2010


..And now for a "proper" implementation of it... Mike, I dont get whats so  
hard.. Perhaps you need to start off basic, and work your way up, instead  
of writing a parser from scratch..

Implemented as a plugin, which you can view here:  
http://dd32.id.au/files/wordpress/example-contributors-rewrite.php

For the sake of people who wont follow links, here's the guts of that  
plugin, without comments, to show the difference to Mikes suggestion:


<?php

register_activation_hook(__FILE__, 'example_activate');
function example_activate() {
	global $wp_rewrite;
	example_add_rule();
	$wp_rewrite->flush_rules();
}

add_filter('query_vars', 'example_add_query_var');
function example_add_query_var($qvars) {
	$qvars[] = 'contributor';
	return $qvars;
}

add_action('init', 'example_add_rule');
function example_add_rule() {
	add_rewrite_rule('contributors/([^/]+)/?',  
'index.php?pagename=contributors&contributor=$matches[1]', 'top');
}

add_action('template_redirect', 'example_output', 50);
function example_output(){

	if ( get_query_var('contributor') ) {
		echo "We're viewing contributor <em>" . get_query_var('contributor') .  
'</em> in this example';
	}

}

?>



On Mon, 07 Jun 2010 18:11:36 +1000, Mike Schinkel  
<mikeschinkel at newclarity.net> wrote:

> Hi Giro,
>
>> Is possible to define a rule, that if I have many pages
>>
>> /contributors/john
>> /contributors/jack
>>
>> Where last name is always different but I want that all refer to same  
>> page,
>> with a possibilitie to take name as parameter.
>>
>> Where I have to start looking ?
>
> URL routing in WordPress can be a real challenge. I've been studying it  
> for well over a year and I still struggle with it so the way I'm going  
> to suggest may not be the best way, but it works for me (modify the  
> function names to suit your preferences.):
>
> ===[Add this somewhere in your page.php template, or other page  
> template]========
>
> <?php echo get_person_slug(); ?>
>
> ===[Add this to your theme's functions.php]========
>
> add_filter('query_vars','my_query_vars');
> add_filter('request','my_request');
>
> function my_query_vars($query_vars) {
> 	$query_vars[] = 'person_slug';
> 	return $query_vars;
> }
> function my_request($request) {
> 	if (isset($request['pagename'])) {	// "pagename" assumes that there is  
> post_type of "page"  called "contributors", i.e. that you've added a  
> page with a slug of "contributors."
> 		$parts = explode('/',$request['pagename']);
> 		if ($parts[0]=='contributors') {
> 			$request['pagename'] = $parts[0];
> 			$request['person_slug'] = $parts[1];
> 		}
> 	}
> 	return $request;
> }
> function get_person_slug() {
> 	global $wp;
> 	return $wp->query_vars['person_slug'];
> }
>
> HTH.
>
> -Mike
>
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers
>


-- 
Dion Hulse / dd32
Contact:
  e: contact at dd32.id.au
  Web: http://dd32.id.au/


More information about the wp-hackers mailing list