Understanding WordPress Hooks

On April 17, 2012, in Research, by Chris Prom

As I noted in previous posts, I am configuring  WordPress network for the new University of Illinois Archives site.  In order to make the generic WordPress install to a fully functional CMS, I will be be selecting a theme framework, customizing the theme, and adding plugins to extend the core WordPress functionality.  Before I start with that, however, I’d like to explain a bit about how WordPress works under the hood.  Over the past year or so I’ve gradually come to see that this knowledge makes one a better WordPress administrator.

Like Drupal and several other CMS systems, WordPress includes a core set of files that dynamically produce the site.  In version 3.13. the core is about nine megabytes in size.

There are many reasons why wordpress is so popular, but underlying all of these reasons is the way that the WordPress ‘hooks’ system operates.  In this post, I’ll explain what the hooks system is, why you should care about it, and how that knowledge can help you select themes and plugins.

What are hooks?

In brief, hooks are simply references or placeholders.  When WordPress encounters a hook, any code that is associated with the hook is executed, ‘hooking’ that associated code into core wordpres functions, and affecting the html and javascript that is sent to the browser.  For example, when the hook ‘wp_head’ appears in the core, WordPress looks to see what functions have been associated with ‘wp_head,’ than modifies the output according to the instructions in the ‘hooked’ function.

To get a little bit technical here, developers use hooks to issue a callback to another function, which then executes some code that you want to pass to the core.   For example, if you put the code:

 add_action('wp_head', 'insert_style_sheet')

into your theme file, you are basically saying that each time time wp_head is encountered in the core,  the ‘insert_style_sheet’ function should execute.   The ‘insert_sytle_sheet’ function itself, would be defined in your theme or plug in. This is a vast oversimplification, but more information about how hooks function is found on the wordpress codex under plugin API an on this tutorial form Karthik Viswanathan.

The WordPress core for version 3.3 includes 1,519 hooks, according to the very useful hooks database maintained by  Adam Brown.

Why we you care?

Basically, we care because the hooks system makes it is very easy to extend, modify, or even replace the core WordPress functionality, while still taking advantage of what the core does best–which is to provide a development framework.  In other words, the hook system allows developers to extensively customize, extend, and modify WordPress’s without changing the core files.  This makes it relatively easy to develop new functions for WordPress, while maintaining forward compatibility.  For example, if I want to add a widget that allows users to search our Archon database, I can write such a widget as a plug in, then include it in our WordPress, without worrying that the changes will get lost the next time we upgrade our WordPress core.  Because of hooks many people that are much smarter or harder working than us have developed themes and plugs in that allow us to do all kinds of interesting things.

How knowledge of hooks helps you select plug-ins and themes

Themes and plugins can contain their own hooks, but the more such hooks they have, the more dependent you will become on the theme or plug in.  This can become a real issue if you ever need to migrate to a new theme, so you should really try to select a theme that uses the core hooks, and does not define a bunch of its own action hooks–or at least does not use them in a way that you cannot live without the snazzy functions they might provide.

The ways in which developers write a plug can also become an issue.  If a developer uses a hook that is deprecated in a future version, the plug in will not work.  Therefore, I am not very keen on using plugs in unless a) updates are issues regularly or the plug in is very widely used or b) the plugin function appears simple enough that I could live without it or fix it if necessary.

 

 

Tagged with:  

Comments are closed.