Explaining Action and Filter Hooks with Laverne and Shirley

Welcome to my analogy

Once upon a time, there was a sitcom on TV called “Laverne and Shirley”. During the intro to the show, we see the two women at work in a bottling plant. As Laverne watches the bottles go past, she removes one of her white gloves, puffs it out and puts it on top of one of the bottles moving past. Laverne waves to the puffy white hand cheerily rolling along the assembly line as the theme music rises and we get ready to see what madcap adventures the pair will get into this week.

Intro cued up to one of the “assembly line” scenes:

Imagine that hooks are like lots of friendly hands reaching up from the “WordPress assembly line” where the assembly line is the parade of steps that WordPress goes through to create your site. Each of these hands reaching up from the assembly line is an indication that something is happening here that someone, someday may want to act upon.

Two Kinds of Hooks: Action Hooks and Filter Hooks

There are two kinds of hooks to cover the two ways in which one can interact with the stream of WordPress code: Filter Hooks and Action Hooks. The two kinds of hooks are very similar in the syntax used to interact with them, but they are designed for different types of interaction (depending on what spot in the stream of code they appear).

Action Hooks: Make Something Happen

Action hooks are placed in the stream of code in places where someone might want their custom code to be triggered to do something outside the stream when the hook comes along. In the Laverne and Shirley analogy, maybe every time the glove that indicates that Laverne and Shirley are getting off work for the day comes along, their supervisor wants to remind them to clock out. This would be an action hook. The hook, when it comes along, triggers an action that’s outside of the rest of the stream of activity.

A WordPress example of this would be the activation hook for plugins. Every time a plugin is activated, WordPress marks that with the ‘activate_PLUGINNAME’ hook (where PLUGINNAME is the slug, or unique name of the plugin.) If the plugin is well-written, the developer probably told the plugin to make something else happen when the plugin is activated, like put a link to the plugin’s configuration page on the screen, initialize the place in the database where its data will be stored, or other similar stuff.

In other words, the plugin developer will tell the WordPress installation (lets call this plugin happyfuntime) to watch for when the hook activate_happyfuntime occurs, then run the code in the plugin that puts a message “Thanks for activating Happy Fun Time! Go to the Configuration Page to setup all the options” on the screen (hopefully making the words ‘Configuration Page’ a link to that page).

Filter Hooks: Change Something Before It Gets Used

Filter hooks are there to let someone’s custom code yank some data out of the stream, modify it (add to it, subtract from it or change it somehow) and then put it back into the stream to go on its merry way to do whatever it was going to do before. In our Laverne and Shirley analogy, if a glove comes along that indicates one of their new cherry sodas (they actually worked for a beer company, but stay with me…), perhaps Laverne is supposed to put a sticker on that bottle that says “Now with REAL CHERRY JUICE!” as it speeds past her. The bottle still continues on its merry way to the next stage where it will be boxed up, loaded onto a truck, etc., but along the way, Laverne has modified the content of that bottle’s label. Of course, anyone who has ever seen an “I Love Lucy” episode (gosh, I’m really dating myself here…) knows that it’s really hard to interrupt a moving assembly line. Not to worry. Computers are really fast, and if all is working well, a filter hook will not noticeably slow down the normal pace of the WordPress code.

A WordPress example of a filter hook would be the filter “the_content”. If one wanted to add to the end of each piece of content a copyright message, they could use this filter to grab the content after it was calculated (for whatever page was currently being viewed), add the copyright message to the end of it, then return the content with the copyright message back to the normal stream of WordPress code, so that the site can put it on the page wherever it was supposed to go.

Once More, In Plain English

So, in a nutshell: An Action Hook says to your site “whenever this point in the code comes around, go over there and do this special thing”; a Filter Hook says to your site “whenver this code comes around, grab the data being shuffled along at that point, change it in these special ways, then return it back to the stream.”

More Than a Lesson In Old TV Sitcoms?

Hopefully this will do more than let everyone know that I’m over 35 and watched a lot of TV as a kid. If you find this post useful, please feel free to link to it, copy stuff from it or tattoo it to your forearm so you’ll always have it handy. If you use more than a tiny bit of it, I’d like it if you could say that you got it from my site. If you’ve read through this and noticed that I made a tragic mistake, kindly email me at [vidrinmr at wfu dot edu] and I will appreciate the feedback!

Happy WordPressing!

Related Posts: