Now that everyone’s finally understanding the benefits of using theme frameworks and the like, I’m noticing a lot more hooks in themes:
theme_before_location(); theme_location(); theme_after_location();
Where location is the location of that particular hook (e.g. header, footer, loop). Now that’s great and all, but here’s a smarter way:
do_atomic( 'location' );
Where do_atomic is:
// Registers three action hooks at once.
function do_atomic( $location = '' ) {
if ( !isset($location) ) return;
do_action( "theme_before_{$location}" );
do_action( "theme_{$location}" );
do_action( "theme_after_{$location}" );
}
Makes for much cleaner template files. The next article on smarter hooks will take this concept to an even higher level.
Smarter hooks » Ptah Dunbar – Web Craftsman, WordPress hacker and …: Smarter hooks. Written by Ptah Dunbar on .. http://bit.ly/bJUEG
This comment was originally posted on Twitter
if you keep publishing posts like this, i might have to subscribe. i don’t code anymore, but i’m trying to keep up with the codes to work my designs around them.
Smarter hooks » Ptah Dunbar – Web Craftsman, WordPress hacker and … http://bit.ly/EEgAQ
This comment was originally posted on Twitter
Smarter hooks » Ptah Dunbar – Web Craftsman, WordPress hacker and … http://bit.ly/SKmmA
This comment was originally posted on Twitter
Smarter hooks » Ptah Dunbar – Web Craftsman, WordPress hacker and … http://bit.ly/1099RO
This comment was originally posted on Twitter
I like the idea a lot, it does make things simpler, but I do have one thing against it. I think theme developers started using before and after because they wrapped a div tag around the element location, instead of going with a container or parent element hook and then attaching each child element with a priority. (Sorry for being vague) How would you suggest Thematic or Hybrid use this? Because they don’t have three hooks in a row, but rather a div tag wrapping the middle one. Should this function also add the div too? Maybe your next post will give some insight into this.
BTW, check out:
if ( !isset($tag) ) return;, $tag should be $location.Good catch.
$tagwas$locationbefore i pasted this code on my blog. updated.I get what your saying about the container element.
do_atomiccould have a second parameter where if used, could surround the main hook location with a container div element or so.Yeah, was going to bitch about this
You’d need to be able to do something like do_atomic(‘footer’, ‘after’)
But, all in all, I’m not sure this makes better code: theme_before_footer() and theme_after_header() might be more readable than do_atomic(‘footer’, ‘before’). I like code when it looks like natural language, sort of.