Hooks, In a Glorious Nutshell

Are you familiar with hooks, actions and filters?

If not, then this might be your seriously-simple primer. Let me quickly give you some thoughts about hooks, actions and filters. You can probably take it from there.

Order of Operations

Websites and any programming (procedural at least) has an order that things are done in. They are done in this same order every time and many pains are made to ensure this.

Think of something simple that you engage with every single day, like starting a car. Insert keys, turn keys, hold for a brief moment, and then release. Car starts. There is no alternative way to do this with your current vehicle (unless you buy a new one with those fancy “auto start” features). This is a procedure, and order of how things are done.

If something is out of order in can confuse and confound the programming. Especially a website, and WordPress is no different. Things all happen in a certain order when a page is created on the sever via php and delivered to the client as html.

The server reads the php and goes through all the commands pulling data from the database and placing it in the proper place. This is great and keeps everything running smoothly and webpages loading nicely and properly.

Ah, the lovely DMV…

So, Hooks…

Imagine now that you wanted to alter or modify how something, somewhere happened (vague, I know) and needed to interrupt or have access to this execution order. This is where hooks come in.

Think of this execution order as a line or queue at the DMV or at a busy clinic. Everyone comes in and takes a number and sits down to wait their turn. Everyone has their own agenda in this case with their individual business not too dependent on each other, and things will move much much slower than your website, but focus on the ordering concept.

Imagine this is your program, everyone has their data and they bring it to the forefront at the needed time and deposit it or something is done with it. A single file line of ‘actions’ that need doing to accomplish the goal of the program/site/business.

This is similar to a program, website or in our case WordPress. Think about the time each number is called. There is a little space before the person with that number gets to the desk, then they do their business, and there is a time after they leave the desk before the next number is called.

These brief time periods are hooks. In programming sometimes they are set aside and a hook is built into the code, so you can do something before the person gets to the desk, or something with the data they bring after they have brought it.

Where Now?

Now that you’ve got the very basics of hooks and how this works, it’s time for you to learn to build your own, or at least experiment a little.

For starters:

  1. Plugin API
  2. Filter Reference
  3. Action Reference
  4. Tom McFarlin’s Primer
  5. Tom McFarlin’s WP Tut’s Guide
  6. Smashing Magazine’s Guide

Get goin’!

One thought on “Hooks, In a Glorious Nutshell

Comments are closed.