[wp-hackers] Addition to template-loader.php

Mike Schinkel mikeschinkel at newclarity.net
Fri Apr 3 17:01:22 GMT 2009


Hi Peter:

It did not occur to me to use template_redirect for two reasons:

1.) Given its name I assumed it was triggering a 301 or 302 redirect (obvious now that I look at it, but the name threw me), and
2.) I didn't think to look at it because it didn't have a clean way to terminate and just triggering an exit is generally bad programming practice except for exceptional cases ("exceptional" meaning "Something's wrong, I have to end", not an alternate an expected logic path.)

The tangible problem with using "exit" is it paints the core into a corner if many plugins and themes use "exit" such that code required to be run after the action in that template-loader.php no in the standard flow of any of the calling code can never be added in the future if backward compatibility to plugins is required. IMO to use exit to control flow is simply bad architecture guidance. My guess is this technique of using exit evolved as a workaround to the fact there was no other way to do it and it's simply that no one has ever pressed the issue about changing core?

Given my learning more about template_redirect here's what I'd ask for instead which should be backward compatible. Add the following code in place of "if ( is_robots() )":

	if (apply_filters('template_redirect',null)===false) {
		return;
	} else if ( is_robots() ) {

This would allow any template_redirect to return false to stop if-then processing instead of executing exit yet any plugin that executes exit will still work. This would allow any new and revised plugins to use the best practice and over time few important pluins would be left using exit. An important tangible benefit is that it will be more obvious to experienced programmers who inspect template-loader.php that template_redirect is how you bypass template-loader.php. And this is such a simple change w/o any downsides that I can see that I hope you'll seriously consider making this change.

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


More information about the wp-hackers mailing list