If you are a WordPress developer or designer or have been messing around in the world of WordPress for any period of time, you have by no doubt heard of WordPress hooks. Hooks are a set of custom written functions that can be added to existing functions in the WordPress core to increase, improve or remove functionality. WordPress plugins make extensive use of hooks to latch onto various portions of the WordPress themes or to the admin interface in order to provide the additional functionality or to perform certain actions in certain parts of the code. If you are looking to understand hooks, learn about all the action and filter hooks and all the deprecated hooks, find the latest changes and understand how hooks can be used in customizing WordPress, I suggest you look at Adam’s WordPress hooks database.
Smashing Magazine has listed 10 Useful WordPress Hook Hacks in which they do some useful things by using the hook functionality. Many of these are already being performed by various plugins and by itself serves no new purpose. However, as a learning tool or as a catalyst for plugin authors to try new things, this list can be very useful. For example, the “Get entire post or page in a PHP variable” is similar to the technique used by Matt’s asides and later replicated in various Asides plugins written for WordPress.
Their explanation of hooks is a bit off. Right at the beginning they get it wrong, here:
That’s not entirely accurate. The hooked function gets executed not when the publish post function is called, but when the code in WordPress does do_action(‘publish_post’). The hook names do not have to match the functions, and not all functions can be hooked. Same goes for filters, you’re not filtering functions, you’re filtering whatever is passed into apply_filters().
This is an important distinction to make, because I often see new plugin programmers trying to hook to function names and then wondering why it doesn’t work.
Other thoughts:
– The example given for number 2 is incredibly brilliant. I can’t believe I didn’t think of it. However, consider caching, wp-super-cache would break this sort of thing.
– Number 8 only gets the body and part of the head, not the whole page. Putting a hook exactly where you need it is much more useful, but if you really want the whole page, no hooks are really needed. Just ob_start(“callback”) in your plugin. You don’t even need the flush, that happens automatically at end of execution. This may make your page seem slower though.