Actions. Filters. Events. Hooks. You read about these terms all the time in tutorials and codex. You see code snippets with add_action
, add_filter
, and apply_filters
. What the heck is a hook? What does it do? And why should you care about learning the event management system in WordPress? Stop pulling your hair out and give me a few minutes of your time to explain it in layman’s terms.
Fact: Code runs in order. It has a sequence to it.
In WordPress, a person requests to view a web page. That request is received by the web server and the file index.php
is loaded into memory and runs. That’s the starting point on the server to launch WordPress.
Line-by-line, the code is executed. Additional files are loaded and executed. Code runs in order. There is a logical sequence that is dictated by the code’s architecture. Keep this in mind.
How does this relate to hooks?
Think about a codebase like WordPress. How do you get your piece of code to run at a certain point in this code sequence? WordPress is already built. You know that you never change its codebase. Right? You never go into WordPress Core and make changes (unless you’re contributing). But you need your code to run when some other piece of code runs. How do you do it?
Let’s illustrate the point I’m making. Let’s say you want to add some class attributes to the <body>
.
If you’re building the theme, you can hard-code them; however, that’s limiting, as other plugins will not be able to add additional ones. Okay, so instead you use the function body_class()
to process, build, and render out the classes.
How then can you add additional class attributes to the body? You use the filter event. Huh?
Code might help at this point. When the theme runs body_class()
, that calls the function get_body_class()
:
function get_body_class( $class = '' ) { | |
$classes = array(); | |
// build all of the classes | |
// .. omitted for brevity | |
$classes = apply_filters( 'body_class', $classes, $class ); | |
return array_unique( $classes ); | |
} |
Notice line 8. There’s a filter event. This event exists to give you and I access at this specific point in the code’s execution. Right here the event fires, as apply_filters()
is an event firing mechanism. On this line, when it runs, the event name “body_class” fires.
What happens when an event fires? WordPress calls each pre-registered callback (function, method, or closure) in order. Each of them run. In other words, when this event on line 8 runs, if you registered your function using add_filter( 'body_class', 'your_callback_function' );
, then it will run.
Why is that significant? It gives you the mechanism to run your code at that specific point in the code’s sequence. It lets you hook into the code and say “Hello WordPress, run my code too…please.” It’s significant because, without it, you do not have the means to insert your code into the sequence. Our ability to customize the website goes out the window.
Does that make sense to you? WordPress is giving you the mechanism to hook your code into Core, plugins, and the theme. You get the opportunity to run your code when you need it to run in order to customize and tailor the website.
“I have had memberships with Treehouse, WPSessions, First Version, and Pippin Williamson’s site. They all provided value to my career, but Know The Code has helped me attain a new level.”
Go Pro to grow your abilities, expertise….and career. #BeMoreAwesome
How do you register a callback?
Up until this point, I’ve talked about the firing sequence and how your code runs. How do you get your callback registered with WordPress?
You use add_{action or filter}
and then pass it:
- the event’s name, for example “body_class”
- the name of your callback
WordPress uses the event’s name as the primary key in the Registry. Your callback is how PHP will look up how to call your function.
The sequence then is:
- You pre-register your callback.
- WordPress adds it to the Registry in the global variable
$wp_filter
. - Then when the event fires, your callback runs too.
Pop Quiz
Let’s test your new found knowledge of WordPress action and filter hooks.
-
What's the intent of a hook?
-
Why should you care about WordPress hooks?
-
How do you register your callback?
Yasir Umair says
Great little tutorial. Thanks for your efforts.