WordPress provides thousands of hooks inbuilt so developers can do actions & filters on various places to achieve their custom behavior in WP.
There are thousands of action hooks & filter hooks in WordPress. Check the full list here.
action & filter both are hooks.
So what are hooks: Hooks are designed to change the default behavior of WP.
Hooks allow you to run a piece of specific code or function at a certain level.
There are 2 types of hooks:
1 – Action hooks
2 – Filter hooks
action is like event. At particular event if you want to run a custom function (call back function) – Use action hook. so action hook add functionality in wp.
filter is like default WordPress’s variable. If you want to change the value of ‘the_title’ or ‘the_content’ – Use filter hook. so filter hook modify value of variable in wp.
Action hooks have 2 methods
do_action() & add_action()
Filter hooks have 2 methods
apply_filter() and add_filter()
Let’s talk a little bit more about each.
Action hooks – Used to add something at a certain point of WP execution. It only adds something and has no return keywords. It can’t modify the existing filter/output.
Some more details about action hooks:
- do_action()
- add_action(‘hook’, ‘call back function’, priority)
Now, what are action hooks? Action hooks are some event or some stage of execution like ‘loop_start’ or ‘loop_end’. There are some pre-defined hooks in WP. We can say those are stages of execution in WP. There are hundreds of hooks in wp. You can see the list here
so by the add_action() hook you can add something when the blog post loos starts or the loop ends.
Add below code in functions.php file and you can see a custom text before content loop started & ended for each page/post. So action hook adding something to default wp flow.
// This is demonstration of action hook
add_action('loop_start', 'add_new_div');
function add_new_div()
{
echo 'Hello world, How are you? I am Dilip';
}
add_action('loop_end', 'close_new_div');
function close_new_div()
{
echo "bye bye";
}
Filter hooks – They hijack some information from WP and modify it (filter it) & give it back to WP. Filter always return something which they modified.
Some more details about filter hooks:
- apply_filters()
- add_filter()
apply_filters(‘filter’, Values to be filtered out)
add_filter(‘filter’, ‘call back function’, priority, number of arguments)
Now, what are filters? filters are like some values in WP for example: ‘the_auther’ or ‘the_title’ or ‘the_content’.
So you can use those filters and pass those into the add_filter() method to change its value. There are thousands of filter hooks in WP. You can see the list here.
About priority
When you want to run more than one ‘call back function’ in add_action() or add_filter(), you need to define priority as an argument value.
The function call will be applied by priority order, which means priority 9 will run first and then 10 and then 11 so on.
so high priority value = later to be run
The default priority number in wp is 10.
In short, the action hook adds something & filter hook modifies something in the WP execution.
Add this code to functions.php file and you can see the content of page/post are altered with custom text. So filter hooks modify the default value of parameters/variables.
// Now demonstration of filter hook
add_filter('the_content', 'modify_content');
function modify_content($content)
{
$content = $content . '<br>This is demo of filter hook.';
return $content;
}
That’s it for now. Enjoy your day!