Yogesh Chauhan's Blog

Actions vs. Filters: hooks in WordPress

in WordPress on July 26, 2021

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.

You can keep the hooks separate kinda (not entirely) like modules in JavaScript.

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.

Actions

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.

Filters

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.

References


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to Use ROLLUP Operator in SQL and MySQL?SQL/MySQLHow to Make CSS Lists Bullets Smaller?CSSHow to auto-resize textarea based on text input using JavaScript or jQuery?JavaScriptHow to add multiple Columns in CSS like Bootstrap?CSSHow to add Lifecycle Methods to a Class in React?ReactIN and BETWEEN Operators in SQLSQL/MySQL