There are two types of hooks in WordPress :
- Actions hooks
- Filters hooks
The basic introduction of these hooks are as follows:
- Actions Hooks- These are the kind of hooks that are used for adding new functionality in wordpress, suppose as we see in last blog how to create custom post type , so in that code you can easily find that we created that by using the add_action. That is not the only action hook which wordpress provides, there are other hooks also in wordpress for performing different actions in wordpress, like remove_action , which is used to removing that specific action which you added by using the add_action.
For example :
In the above example, you can clearly see that for enqueueing the new style we are using the action hook add_action.
2. Filters Hooks – These are the hooks that are basically used for modifying the inbuilt functionality or existing functionality in WordPress. Basically, we can understand by its name that filter means choosing something from most of the things or a bunch of the things.
So, exactly the same work can be done by the filters in WordPress.
For example: suppose someone adds some comment to your post and you want to change but don’t have their username and password, but you have the access to the c-panel or code of your website you can change their comment by using the filter. Show the code below:
// Cut Jack's boasting
add_filter( 'jacks_boast' , 'cut_the_boasting');
function cut_the_boasting($boast) {
// Replace "best" with "second-best"
$boast = str_replace ( "best" , "second-best" , $boast );
// Append another phrase at the end of his boast
$boast = $boast . ' However, Gill can outshine me any day.';
return $boast;
}<br>
In the above code, you can easily see how a developer can change the existing comments by using the filter.
The popular use of filter we can understand when we are using some big plugins like Woocommerce, Paid Membership Pro, these are the plugins where we have to modify their functionality if we have to make them according to our website and our manage our store in the case when we are using the woocommerce.
When we have to add some extra fields in our checkout form we are using the woocommerce inbuilt filter function to modify the default functionality of the woocommerce plugin.
So, we can easily understand what are the hooks in wordpress and how we can use them for the betterment of our website.