Thord Daniel Hedengren - Smashing WordPress_ Beyond the Blog-Wiley (2014)

(avery) #1

156 PART II • Designing and Developing WordPress Themes


the question of whether said settings should be a theme options page at all, though, as most
things that are not visual are things that need to travel to the next theme as well, and thus
would belong in a plugin. I’ll talk about portability in later chapters, but it is worth consider-
ing. I’d say that settings that feel a bit stretched in the Theme Customizer should either be
scrapped altogether or moved to a plugin, but your experience may vary.

MASTERING ACTION HOOKS


Action hooks are all the rage these days, just like theme frameworks. Actually, they are
probably so hot because of the theme frameworks, because the frameworks are littered with
these babies. But I’m getting ahead of myself. First you need to understand what an action
hook is.

Your theme is most definitely already using action hooks. The familiar wp_head and wp_
footer calls in the header.php and footer.php template files are action hooks. They are there
so that developers can hook in and add stuff to the beginning of WordPress or after it has
finished loading. Plugin developers use these all the time, and I imagine that any of the web
statistics plugins utilizing Google Analytics, Woopra, or whatever hook on to either the
wp_head or wp_footer action hook and add their tracking code. But that’s not all: A lot of
hooks actually happen when standard template tags are used, such as wp_meta(). Chances
are you can hook on to most of the template tags in your theme already.

You can add your own hooks as well. This is the primary way theme framework designers use
hooks — basically adding a number of hooks to various parts of the theme and then letting
the user or child theme developer alter the look and add features by hooking on to them.
Hooks can be used for a multitude of tasks, from populating a menu or advertisement spot
with proper coding to actually removing elements from the design. In a sense, using hooks is
a good way to make a very complete design and then let the users decide which parts of it
should be used. Add an admin options page that does this for you, and you don’t have to
worry about hacking functions.php as a user, as naturally that’s where all the action is.

I tend to lean the other way, though. Although it may be tempting to just add everything
imaginable and then have the users check or uncheck boxes to decide what should be dis-
played, this practice can quickly bloat the theme. In my opinion, a good framework is
something to build on, not to cut away from.

Either way, with action hooks, you can add functionality, either by adding your own (you’ll
get to that in a little bit) or by freeloading the hooks from WordPress. There are a lot of
piggyback possibilities within WordPress, with helpful hooks such as wp_head (which you’ll
find referenced in most themes’ header.php file) and wp_footer. There are way too many to
list here, so I’ll just point you to the (hopefully up-to-date) Action Reference page in the
WordPress Codex: http://codex.wordpress.org/Plugin_API/
Action_Reference.
Free download pdf