I have covered type of hooks in a small section of WordPress plugin intro post.
Hooks gives you an ability to modify some code using another piece of code at some specific spots.
Hooks are very important part of the overall WordPRess development, whether it’s a theme or a plugin.
Hooks make it possible for themes and plugins to interact with the core WordPress.
There are 2 types of hooks: Actions and Filters.
Steps to follow to use Actions or Filters hooks:
- Write a custom function (Callback).
- Register the custom function with a WordPress hook either for a specific action or a filter.
If you want to add data into the database, use actions. If you want to show some output at a specific point, use actions.
Actions callback functions do not return anything back to the calling Action hook.
To change data while executing a plugin or a theme, use filters.
Filters callback functions work completely separately without affecting global variables.
Filters callback functions will return the variable passed. You can modify the variables passed into the function too.
What are the differences then?
Actions => takes variables => uses it => no return value
Filters => takes variables => modifies it => returns the variable
Actions callback functions enters into WordPress flow to perform some kind of “actions” and then leaves the WordPress flow.
Filters enters in the WordPress flow and then leaves some changes behind so that you can use those changes later on.
actions filters functions hook types