BLOG
Use WordPress Actions and Filters for Custom Functionality

Modify Your Site’s Behavior with Hooks!

Hook into actions and filters for custom functionality

In your hunt for plugins or themes for your WordPress site, you’ve probably come across some options that are almost perfect, but not quite. Maybe they’re missing one key feature, or maybe they don’t send along quite the right information you need. If you find something close, don’t just give up and throw it all away! Check the documentation or files to see if there are any actions or filters you can hook into instead, and create that extra functionality yourself.

The basics of hooking into actions and filters

Essentially, WordPress (along with many of the plugins and themes developed for it) have built-in hooks that you can “hang” your own code onto written into the code. Oftentimes, these are initially used by the developer for their own logistical purposes. Instead of clogging up multiple pages with the same function over and over, they create an action or a filter that they hook into. This means that the big block of code is only stored in one place, and every place that needs it only has a single line referencing the action or filter.

Thankfully, this also allows for end-users to hook into these actions or filters and customize the functionality for their own needs! WordPress itself has tons of these with great documentation, and bigger plugins like WooCommerce have extensive documentation as well. However, even if a plugin or theme doesn’t everything written out on their site, it’s still not too hard to find the information you’re looking for. But first…

What’s the difference between an action and a filter?

There are two types of hooks that you can link to: actions and filters. The process of using these hooks is similar,  but there are a few key differences in their capabilities:

  • Actions, as the name implies, allow you to do something. They pause the code execution, run your specified callback function (more on that later), and then resume the code. They don’t return anything back to the hook, everything that happens is contained within the callback function. Actions are what you use to add extra functionality to the page – a search bar or menu that was missing, a promotional message into the middle of the code block that you wouldn’t be able to access normally, etc.
  • Filters are for modifying data, rather than adding functionality. A callback function hooked to a filter should take a variable (or variables), modify it, and then return it. The filter hook expects to have data returned to it. This can as simple as adding a CSS class to an element, or as complex as returning a user’s unique ID that defines the entire content of the page.

How do you find available hooks?

As mentioned above, there’s a good chance you can find some explicit documentation on the hooks available to you for any given theme or plugin. But this kind of documentation takes time, and not every developer has it available. In that case, it’s not a big secret, it just takes a little legwork on your end. Open up some PHP files, and look for lines of code that look like this, for actions:

do_action( 'action_name_here' )

And this, for filters:

apply_filters( 'filter_name_here', 'filter_variable_here' )

Hopefully, the names of the files and hooks will give you a good idea of what they’re used for, and if they’ll be useful to you. But even if the names aren’t helpful, you can hook into them anyway with a simple callback function and see what happens!

How do you hook into actions and filters?

Once you’ve honed in on your hooks, you have to actually link into them. The first step is to write the PHP code for what you want to happen, and wrap it in a function. For actions, this might be to echo a promotional message, or add a new widgetized area. For filters, this could be to search through some data to find the variable you want to return. Whatever it does, be sure to name the function well, so you and anybody looking at your code will have an idea of what it does. Then, when your callback function is all written and ready, you can hook directly into the action or filter necessary with a single line of code. For actions:

add_action( 'action_name', 'callback_function_name' )

For filters:

add_filter( 'filter_name', 'callback_function_name' )

Note, you can use the same callback function with multiple hooks. Also, on a more advanced note, you can use multiple callback functions in one hook, specify the priority to direct the order they run in, and/or pass multiple variables into a hook. These advanced features are done with a few extra arguments in the above lines:

add_action( 'action_name', 'callback_function_name', 10, 2)

In the above example, 10 is the priority (low, in this case), and the 2 is the number of variables expected to be passed into the callback function. These can be altered to fit your needs of course, but in most cases, you’ll be sticking to the first two basic examples.

What can you do with these hooks?

Like most customizations with WordPress, you’re only limited by your own capabilities. Just like with custom plugins or custom themes, just about anything is possible with the right hook and some knowledge of PHP. There were a few examples mentioned about, like adding a promotional message or setting a user ID. But these were just a few basic examples to get you inspired. If you can think it, it’s probably possible! And at Mr. WPress, we’re here to help. Need some help with hooks, either finding them or using them? Reach out to us for a free quote to see how we can help you create the perfect custom website.

RELATED BLOG POST