What are WordPress hooks? How do actions and filters help to extend the functionality?

0
1091
WordPress Hooks | LearnWoo

If you have ever worked on WordPress, you must have come across the word ‘Hook‘ a lot of times. If you are a WordPress developer who wants to build plugins and themes, then WordPress hooks are crucial for your development.

But what exactly are Hooks?

According to WordPress.org codex, “Hooks are provided by WordPress to allow your plugin to ‘hook into’ the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion.”

Hooks in general terms connect one thing with another. In WordPress terminology, it hooks(connects) user-defined code with existing code of the WordPress core.

Let’s just get this straight.

WordPress is built with some default functionality. It has default behavior and values associated with it. With hooks, you can modify these default behavior and replace the existing value, hence customizing the WordPress environment as per your needs.

Types of Hooks

There are two types of Hooks:

  1. Action Hooks – Used to execute at some point in the execution, changing the default flow of execution. Here’s a list of WordPress Action hooks.
  2. Filter Hooks – Slightly different from Action hooks, Filter hooks are used to modify the default WordPress data and replacing it with custom value, before it is shown to the user in the front-end, or stored in the database. A list of Filter hooks can be found in the WordPress Codex.

Basically, hooks are predefined functions in itself. Hence the two types of functions associated with the hooks are:

  1. Action functions – These functions are helpful in events when a blog is posted, a page load and so on.
    Following are some of the Action functions:

  2. Filter functions – Every filter function returns some value and replaces it with the default WordPress data.
    Filter functions include the following:

An Infographic

Observe the following infographic for better understanding on how hooks work.

WordPress Hooks | Info-graphic on how WordPress Hooks work
Info-graphic on how WordPress Hooks work

As shown in the above infographic, the Action and Filter functions make use of respective hooks to modify the core code of the WordPress.

How does a Hook look like?

A typical Action hook takes the following form:

add_action ( ‘hook’, ‘your_custom_function_name’, [priority], [accepted_parameters] );
function your_custom_function_name()
{
   //Your custom code
}

Where hook is the name of the hook, your_custom_function_name is the name of the function with your custom code, optional priority parameter which indicates how the hook should be executed, accepted parameters indicating the additional parameters, and the custom code. The function call (add_action()) can be below or above the code snippet.

A Filter hook is of the following form:

add_filter( ‘filter’, ‘your_custom_function_name’, ‘priority’, ‘accepted_args’ );
function your_custom_function_name($variable)
{
   //Your custom code
   return;
}

Where add_filter() is one of the Filter hook functions, filter is the name of the filter you are going to use, your_custom_function_name is the name of your user-defined function, priority parameter indicates the execution order of the hook, accepted_args indicates arguments to be passed, and the custom code with the mandatory return value.

Just like WordPress, there are hooks available for WooCommerce as well. These hooks are extensively used to develop plugins, themes, and customize the WooCommerce Checkout process.
Read WooCommerce Hooks: Actions and filters in the WooCommerce documentation to understand the WooCommerce hooks in detail.

Demonstration

Let’s understand hooks using some sample code snippets.

Action Hook and Function

//Adding an Action hook
function my_action_hook() { 
   echo "<p align='center'>This is an Action Hook Example</p>";
}
add_action( 'get_header', 'my_action_hook' );

In the above code snippet, ‘my_action_hook’ is the name of the custom function, ‘add_action’ is one of the Action functions, and ‘get_header’ is used to customize template header. In the given code snippet, we are displaying the text ‘This is an Action Hook Demo’ in the header section of the website.

A sample screenshot showing the applied code snippet changes is shown below.

WordPress Hooks | Action hook example
Action hook example

Filter Hook and Function

//Adding a filter hook
add_filter('woocommerce_get_price_html','my_filter_hook');
function my_filter_hook($price){ 
    return 'For just '.$price;
}

In the above code snippet, ‘woocommerce_get_price_html’ is a WooCommerce hook to get product price, ‘my_filter_hook’ is the name of the user-defined filter and ‘$price’ is an user-defined variable. The code snippet simply adds the prefix “For just” to the product price.

Following screenshot shows how the code snippet affects our WooCommerce store.

WordPress Hooks | Filter hook example
Filter hook example

This is just a simple example, there’s a lot of things you can achieve with Hooks.

How do actions and filters help to extend the functionality?

  1. The primary advantage of using Hooks is that it helps in customizing the core WordPress code.
  2. Any changes in development can be carefully monitored as the modification is only minimal rather than on multiple occasions in the code.
  3. New plugin development is made a lot easier with the try-and-test method.
  4. For theme development, one can create child themes and play around with hooks without affecting the parent theme.
  5. It is simple, easy to use WordPress offering that has understandable structure and detailed documentation for any reference.
  6. Supports WooCommerce, the most popular WordPress eCommerce plugin. WooCommerce pricing plugins extensively change product prices, price calculations, and offer dynamic pricing & discounts using WooCommerce hooks. Order details, product information, shipping details among things can be tapped using custom hooks.

Concluding Comments

WordPress Hooks are the key mechanism for plugin and theme development. They are widely used to modify the existing WordPress code to customize the website. While hooks have a number of advantages, one should make sure that each custom hook is unique and does not overlap with another hook of the same name. Even in such case, you can assign priority to the hooks for execution as per your needs.

Check out our WooCommerce Customization articles for tips on customizing your WooCommerce store.

Or continue exploring LearnWoo for more amazing articles.