What are Hooks, Filters, and Actions? (Simple Guide)

Have you seen function like add_filter() or add_action() scattered across your Theme files? They are called Hooks and we will learn what exactly those are.

Have you seen function like add_filter() or add_action() scattered across your Theme files?

They are called Hooks and we will learn what exactly those are.

1. What are Hooks?

The main idea is that if you want to customize a plugin, you should not change the files directly because your changes will be overwritten when you update that plugin.

The same logic goes to WordPress core files.

Luckily, plugin maker can add Hooks so we can customize them within our own code.

There are two types of Hook:

  • Filter – to change the value of a variable.
  • Action – to run a function at a certain place.

2. Creating a Filter

Let’s assume we have created an eCommerce plugin. The default currency is “$” and we want it to be customizable:

$currency = apply_filters( 'my_currency', '$' );

// 1st param is the name of the filter
// 2nd param is the default value

Hooking into that filter:

add_filter( 'my_currency', function( $currency ) {
  return '€';
} );

// Or in older PHP syntax:

add_filter( 'my_currency', 'change_currency' );
function change_currency( $currency ) {
  return '€';
}

Now the currency is changed to € (Euro).

3. Creating an Action

We have a Header template and we want developers to be able to add extra elements in before or after the navigation menu:

<header class="site-header">
  <?php do_action( 'before_nav' ); ?>
  <nav> ... </nav>
  <?php do_action( 'after_nav' ); ?>
</header>

Hooking into that action:

// Logo before nav
add_action( 'before_nav', function() {
  echo '<img class="logo" src="...">';
} );

// Breadcrumb after nav
add_action( 'after_nav', function() {
  echo '<div class="breadcrumb"> ... </div>';
} );

With the snippet above, we added Logo before navigation, and Breadcrumb after it.

4. Hook Priority

A filter and action can be hooked multiple times. It will run them in the order of their priority ascendingly.

By default, the priority is 10 and you can change it by adding 3rd parameter:

// These codes follow the scenario from No.3

add_action( 'after_nav', function() {
  echo '<div class="breadcrumb"> ... </div>';
} );

add_action( 'after_nav', function() {
  echo '<div class="search-bar"> ... </div>';
}, 5 );

The breadcrumb’s hook doesn’t have 3rd param, so the priority is 10.

Meanwhile, the search bar’s hook is set to 5 so it will appear first.

5. Multiple Hook Parameters

You can pass in more than 1 variable when creating a filter or action. In that case, you need to define how many as the 4th parameter.

EXAMPLE:

We have a Core Filter called the_title. It is used to modify the post title and passed in 2 variables: the current title and post ID.

Documentation of the_title filter in wordpress.org

If you want to use the ID, you need to add 4th parameter in your add_filter() like shown below:

add_filter( 'the_title', function( $title, $id ) {
  $thumbnail = get_the_post_thumbnail( $id );
  return $thumbnail . $title;
}, 10, 2 );

Now the title will have its featured image before it.


Conclusion

Hook allows you to modify Plugin or Core files within your own code. So when the Plugin or WordPress is updated, your changes still stay intact.

Although the code samples given are very short, I believe they are powerful enough to teach all you need to get started.

Let me know in comment if there are some part that you need more explanation 🙂

Default image
Henner Setyono
A web developer who mainly build custom theme for WordPress since 2013. Young enough to never had to deal with using Table as layout.
Leave a Reply