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 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #3 How to add Read More Read Less Button using JavaScript? #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Aug 15 Is PHP still good for back-end programming? #Aug 10 How to create a multisite network in WordPress? #Aug 3 How to create a circle that follows a cursor using JavaScript and CSS? #Aug 3 How to make a curtain slider using jQuery and CSS? #Aug 2 How to progressively load images and add a blurry placeholder? #Aug 1 How to create a placeholder loader (throbber) using CSS?
You might also like these
How to draw an SVG path on scroll using JavaScript?JavaScriptHow to make WordPress main stylesheet (style.css)?WordPressHow to enable and disable button using JavaScript?JavaScriptWhat are components in Angular?AngularCROSS JOIN in PostgresPostgresHow to create a Bootstrap style accordion using CSS and JavaScript?CSS