You’ve most likely seen code within WordPress that uses add_action or add_filter. In this article we take a look under the hood of WordPress and take a dive into actions vs filters. This is a key component that you need to understand, to improve your skills as a developer for WordPress, even if you are just starting out.

 

WordPress Actions

A WordPress action is usually areas in WordPress, WordPress themes or plugins that allows you to add additional functionality. There are thousands of actions inside WordPress that allows you to extend the functionality of WordPress. Some scenarios could be: when WordPress is loaded; Add code to <head> or <footer> tags inside WordPress and many more places.

 

WordPress Filters

A WordPress filter is similar to an action, but the major difference is that a filter will edit data when called. The data will be edited either before inserting to the WordPress database or after retrieving the data and displaying it. Some scenarios could be: Change the excerpt length; Adjust a product’s price in WooCommerce and many other areas of WordPress plugins and themes. A filter should always return a value – this can be found at the bottom of the function.

 

What Are Hooks?

Hooks are simply the collective word for WordPress actions and filters.

Hooks are a way for one piece of code to interact/modify another piece of code. They make up the foundation for how plugins and themes interact with WordPress Core, but they’re also used extensively by Core itself.”taken from the WordPress Plugin Developer Handbook.

 

Summary Of Actions Vs Filters

If you can grasp this concept at the very core, this will help you become a great WordPress developer. Once you start working with this you should start getting more comfortable and you will be able to customize WordPress the right way without editing core files.

  • Actions – Adds functionality to the specific area called.
  • Filters – Changes data value before saving or displaying the data.

Now I’m sure you are eager to get WordPress development on and wondering “this is great, but where can I find out what actions and filters are available?”. The answer is simple, use the WordPress developer’s handbook – this will probably become nearly the most visited site when developing, coming close to the amount of times you would visit StackOverflow.

Photo by Saffu on Unsplash