WordPress Actions and Filters: A Beginners Guide
I been asked by a lot of visitors if I could do a guide to Actions and Filters for those just starting out with WordPress development. So, I thought I’d give it a try.
I’ve Heard Talk of Hooks. What Are Hooks?
Well WordPress have a small entry about this in their Codex. Hooks, or sometimes Hooks & Filters, are actually just different names for Actions and Filters.
What Are Actions and Filters?
Good question. Actions are points in the WordPress core, and sometimes plugins & themes (depending on the author), where you can hook in (hence the term hook) and have the code run a function of your choosing.
Filters are similar to Actions. The difference is that a Filter will return data, normally the output data of a function, and allow you to alter it & then return it to be used as normal.
Can I Have An Example?
Of course. Let’s try an Action first.
Example: Actions
The most commonly used action is probably wp_enqueue_scripts
this is the Action you should use to attach scripts & CSS stylesheets. Use it in your plugin, or in the functions.php
file of your theme.
1 2 3 4 |
function my_enqueue_script() { wp_enqueue_script('jquery'); } add_action('wp_enqueue_scripts', 'my_enqueue_script'); |
There we have it. In this example I’ve just instructed WordPress to attach it’s built in version of jQuery. If you want to enqueue your own scripts using wp_enqueue_script()
check out the WordPress Codex.
Example: Filters
Now for an example of a Filter. One of the most common examples is changing the ‘more’ character of the excerpt.
1 2 3 4 |
function my_excerpt_more( $more ) { return '…'; } add_filter('excerpt_more', 'my_excerpt_more'); |
Here you can see there is only a slight difference between an Action and a Filter. The function created for a filter receives a value through a parameter. For those unfamiliar, in this example, the parameter is the ( $more )
part of this function. Parameters bring a variable in to the scope of a function so that it can use, or alter, it’s contents should you need to. For example, in this function $more
contains the current ‘more’ character.
Going back to the function. When you add a function to a filter you need to know the parameters it gives you. You can find this by using this brilliant hook database. Just find the filter you want, click it, then click ‘View in Source’. Then just look for a line that looks similar to the following:
1 |
apply_filters('excerpt_more', ' ' . '[...]'); |
Remember the line you are looking for will start apply_filters
, but the rest will be slightly different.
The number of commas after the filter name, in this case 'excerpt_more'
, determines how many parameters there are. For this filter there is only one (that period is for concatenation).
So, Filters are the same as Actions?
An important thing to remember is that the last thing a Filter must do is return a value. WordPress is expecting a value to be returned, whether that be the default value or your modified value. Actions do not necessarily need to output or return a value in order to work. So, no. Although they may look similar, Filters aren’t the same as Actions.
Summary
To sum up, Filters & Actions are used to alter returned data & trigger functions respectively. These are very, very useful and are one of the best developer features in WordPress (in my opinion). In fact in some theme frameworks (Genesis) and plugins (WooCommerce) it is nearly impossible to avoid using Filters & Actions should you wish to develop for them.
This guide was aimed at those just starting out so if you are confused about anything please leave a comment & I’ll try my best to help rescue you from the castle of confusion. As always, thanks for reading.