WordPress Hooks: A Beginner’s Guide
Trying to make some changes to your WordPress website? HTML and CSS can only go so far.
If you need to make changes to how WordPress behaves, and you can’t find a theme or plugin that does what you want, then WordPress hooks might be the answer you’re looking for.
Understanding hooks is an essential step in learning how to modify or create plugins and themes. But if you try searching for answers to what exactly hooks are and how they work, you’re likely to find mostly technical articles written for developers.
What about if you’re a beginner, or don’t know how to code, and want to understand hooks?
This article will explain WordPress hooks for beginners, and teach you how you can use them to confidently modify your own WordPress site.
Almost everything in WordPress works with hooks, so once you understand this concept, you can modify pretty much anything.
What are WordPress hooks?
Imagine your WordPress website as a giant, mixed-media collage on a wall. Mixed in with the splashes of paint, upcycled greeting cards, and magazine cutouts (your theme, plugins, and content) are some hooks.
Those hooks are there so you can add your own artwork to the collage, customizing it as you please.
WordPress hooks work kind of like that, allowing you to “hang” your own code in certain spots.
You can also think of hooks as triggers. Depending on the specific hook you use, your code will be triggered in certain areas.
Here’s how the WordPress codex describes hooks:
“Hooks are provided by WordPress to allow your plugin to ‘hook into’ the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion.”
There are two kinds of hooks: actions and filters.
A WordPress action hook metaphor
Action hooks set custom functions to trigger with specific WordPress events.
Let’s say you’re waiting on an important package to arrive at your house on Saturday, but you have errands to run and can’t wait around at home to sign for it. So you go to your neighbor, Sara, and ask her to sign for your package when it arrives.
If your request were a WordPress hook, it would look like this:
function sign_for_package( $sara_is_home ) { if ( $sara_is_home ) { echo 'I’ll sign for that package!'; } } add_action( 'when_package_arrives' , 'sign_for_package', 10 , 1 );
In the above example, the code waits for the package to arrive, and then it sends Sara to sign for it (if she’s home!).
What are the numbers 10 and 1?
The first number sets the priority of the function: should it be performed before or after other functions? The default is 10, so we’ll leave it at that. Most of the time this won’t make a difference.
The second number is the number of arguments or variables in the function. We’re just using the variable $sara_is_home, so it’s set at 1.
A WordPress filter hook metaphor
Filter hooks are functions that change or filter data, just before taking action with that data.
Let’s say Sara agrees to sign for your package, if you agree to quiet your dog down next time it starts barking too loud.
We can create a filter hook for her request:
function quiet_down($dog) { $dog = str_replace ( "bark" , "silence" , $dog ); return $dog; } add_filter( dog_barking' , 'quiet_down');
With this filter, the trigger is the dog barking. When that happens, each bark is replaced with silence.
This is a filter, not an action, because we’re changing something that already exists (barking into silence), instead of adding something new.
A useful, real-life example
“So,” you might be thinking, “I really wish I could stop my neighbor’s dogs from barking with a WordPress filter – but I can’t. What are some practical uses for actions and filters?”
Add content after every post, even in RSS
Say you want to automatically add a bit of content after every single post.
You can do that by modifying your single.php template file, or by using an after post widget area. But with either of those methods, your new content won’t show up in your RSS feed.
In order to modify the content so it shows up in your RSS feed, you can use a filter:
function after_post_content($content) { if(!is_feed() && !is_home()) { $content.= '<h4>Like this post?</h4>'; $content.= '<p><a href="http://example.com/subscribe">Subscribe to our newsletter</a>!</p>'; } return $content; } add_filter ('the_content', 'after_post_content');
This is a filter, not an action, because you’re modifying the content of your post, not adding a completely new function.
WordPress hooks can be added to the end of your functions.php file in your current theme directory. You can also use them to create your own plugins. Be sure to backup your site before making any changes.
A few things to keep in mind
Review the PHP Coding Standards for WordPress
Before you begin to create your own actions and filters, take a look at the PHP Coding Standards for WordPress.
WordPress is a collaborative project, and your functions will be interacting with not only the WordPress core but also other themes and plugins. It helps everyone to get along and play nicely together if we’re all using the code in a consistent way.
Use a unique name for functions
When creating your own hook functions, be sure to use a unique name. If you happen to use a function name that’s already in use somewhere on your site, it could bring your whole site to a screeching halt.
The standard practice for this is to use a prefix that matches the name of your theme or plugin. For example, if you’re creating a plugin called Awesome WordPress Plugin, you could prefix all your functions with awp_.
Use plenty of comments
Comments are not only useful for others taking a look at your code, but for yourself, too! You may need to go back and modify your code months or years later, and forget exactly what you did. Comments also make it easier to read and understand code quickly. To save yourself time and headaches, use comments to explain what’s going on with the code, even if it seems obvious to you now.
Conclusion
WordPress is easy to modify with the right themes or plugins, but sometimes you want to make a unique change that you can’t find a theme or plugin for. By learning how WordPress hooks work, you’ll have a greater understanding of WordPress as a whole.