[wp-hackers] Options for Controller - Views

Mike Schinkel mikeschinkel at newclarity.net
Fri Nov 20 16:55:28 UTC 2009


On Nov 20, 2009, at 11:17 AM, Otto wrote:
> On Fri, Nov 20, 2009 at 9:29 AM, Mike Schinkel
>> Yes it is a nasty, dirty, ugly hack.  In order to get something as simple as an arbitrary URL to route requires use of the follow hooks (directly from my plugin):
>> 
>>                add_action('template_redirect',array(__CLASS__,'custom_url'));
>>                add_action('generate_rewrite_rules', array(__CLASS__,'add_rewrite_rules'));
>>                add_filter('query_vars', array(__CLASS__,'add_queryvars' ));
>>                register_activation_hook(__FILE__,array(__CLASS__,'flush_rewrite_rules');
>>                add_action('pre_get_posts',array(__CLASS__,'set_query'));
>>                add_action('parse_query',array(__CLASS__,'parse_query'));
>>                add_filter('the_content',array(__CLASS__,'filter_post_list_content'));
>>                add_filter('posts_fields',array(__CLASS__,'filter_posts_fields'));
>>                add_filter('posts_where_paged',array(__CLASS__,'filter_posts_where_paged'));
>>                add_filter('posts_join_paged',array(__CLASS__,'filter_posts_join_paged'));
>>                add_filter('posts_orderby',array(__CLASS__,'filter_posts_orderby'));
>>                add_filter('posts_groupby',array(__CLASS__,'filter_posts_groupby'));
>> 
>> It is total crap that you have to use so many hooks to get the path "/products/{product}" to work and that your hooks have to much with fragile SQL.
> 
> Sweet Jebus, man. Why in the *world* would you need all that?


To create a generic functionality that lets me add URLs like what follows and have them load appropriate functionality from WordPress such as a post or a list of posts:

add_custom_url(	array(
	'name' 					=> 'solutions-list-by-alpha',
	'url_type'			=> 'post-list',
	'url_template' 	=> 'solutions/by-alpha',
	'template_file' => 'custom/solutions-by-alpha.php',
	'mf_page_type'	=> 'Solution',
));

add_custom_url(	array(
	'name' 					=> 'solution-page',
	'url_type'			=> 'single-post',
	'url_template' 	=> 'solutions/{solution_slug}',
	'template_file' => 'custom/solution.php',
	'mf_page_type'	=> 'Solution',
));

add_custom_url(	array(
	'name' 					=> 'solutions-list',
	'url_type'			=> 'post-list',
	'url_template' 	=> 'solutions',
	'template_file' => 'custom/solutions.php',
	'mf_page_type'	=> 'Solution',
));

add_custom_url(	array(
	'url_template' 	=> 'test',
	'callback' 			=> 'do_test_page',
));

function do_test_page() {
	echo 'Hi!';
}

> Unless you're doing something completely absurd, I have no idea why
> you need all those hooks. Heck, all the posts_* hooks your using have
> absolutely nothing to do with rewriting or routing of the resulting
> page. Those modify the WP_Query only.

You know, I thought the same until I tried to make it work. I spent a week coding that and every time I got close it seemed like WordPress was actively trying to thwart me.

I also had to kill these which I hated but it was too much to overcome and I didn't have time to do it "right":

			remove_action('template_redirect', 'redirect_canonical');
			remove_action('template_redirect', 'wp_old_slug_redirect');

The reason I needed to muck with SQL is because, while I could get the URLs to route with just a few hooks, I couldn't get it to select the appropriate posts and/or post lists (I was using More Fields plugin to create unique "content types.")  Yes, it's not exactly a routing issue but without the route being able to access WP functionality to pull the appropriate posts then just having custom URL routes is like having a Bugatti Veyron but no gasoline. 

Listen, if you really can make this work easier, and I mean really work, I'm all for you proving me wrong (but you won't in this case, I'm certain of it.) OTOH, if core can be modified so that a route can be specified and tied to a custom post type and/or a custom function all without having to do anything more than specify what I have specified above then that is what we need.

-Mike Schinkel
WordPress Custom Plugins
http://mikeschinkel.com/custom-wordpress-plugins/







More information about the wp-hackers mailing list