Smarter Hooks

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.

4 Tweets

8 Comments

  1. July 1st, 2009 at 12:14 pm | Permalink

    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.

  2. July 1st, 2009 at 10:23 pm | Permalink

    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.

  3. Ozh's Avatar Ozh
    July 2nd, 2009 at 7:19 am | Permalink

    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.

Additional comments powered by BackType

About Ptah

I’m a 21 year old web developer, entrepreneur, and founder of Design by Craftsmen, web creative studio specializing in custom WordPress solutions. Get to know me.

Twitter