jarednova/timber — pull request #68 by mgmartel (2013-09-28 15:44:20+00:00)
Title: Use wp() instead of manually querying posts in Timber::load_template

Sorry for making a pull request on the same bit of code again, but I realized that the logic used in load_template does not reflect the way WP loads templates enough. (almost, though)

Most salient is that because template_redirect is not called, the admin bar doesn't show. But also, a lot of plugins rely on that hook to set up global vars or do further set up (WooCommerce and BuddyPress for example).

Also, no headers were sent because the header in the method was hooked to send_headers, which was never called. No big deal, unless you want to (say) add extra cache headers.

Other potentially useful hooks missed are pre_get_posts and wp.

Instead of calling them manually, we can really emulate the way WP loads templates by using the WP object itself. Only difference really is that we're injecting our query into the object, and preventing WP from parsing the request itself (that's the whole point of the router).

As far as I can tell, there are no further repercussions to this change, except that if other plugins use template_redirect to push their own templates, their templates have precedence of the route. But this is expected behaviour, I think. Better yet, it allows you to do fancy things like build routes on top of plugin pages, like BuddyPress.
If someone does not want this to happen, they can always remove the plugin action.

Now the router really is a full-fledged alternative to wp_rewrite :)

Ps. Added bonus: if the template is not located, the query is still passed in the same way, and WP only takes over the template loading, as a fallback.

Thanks again for an awesome library Jared!