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 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

#Dec 4 What is Etrieve Flow? #Dec 2 The unique operator($) in Envision Basic #Nov 25 Steps to Install Microsoft SQL Server on a MacOS #Nov 11 What is DevOps? #Nov 10 The * arithmetic operator in Envision Basic #Nov 10 What are Big Data Clusters in SQL Server?
You might also like these
What is the difference between let and var in Swift?SwiftConfiguring Modules with @forward rule in SCSS (Sass)SCSSHow to add multiple Columns in CSS like Bootstrap?CSSHow to change style of all direct children of an element using jQuery?jQueryFile System Integrity: How to Keep an Eye on Your Files and Folder Change?MiscellaneousWhat happens when we add Numbers and Strings in JavaScript?JavaScript