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

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
Always add associated labels to your Form elementsUI/UXHow to add AppRoutingModule in Angular application using command line?AngularHow to pass arguments in SCSS function?SCSSData Flows in ReactReactThe SQL EXISTS OperatorSQL/MySQLHAVING Clause in Postgres with Examples in All Aggregate FunctionsPostgres