Filters and Actions in WordPress

The WordPress API supports what it calls “hooks,” ways to invoke a plugin from a website. This allows special treatment of elements in a post or page to help customize the site. These are pieces of code in the PHP programming language that direct the use of the plugin.

Hooks come in two kinds: Filters and actions. They’re somewhat interchangeable, but they apply to different use cases. WordPress makes certain hook names available, and a hook connects a function (a block of code that can be invoked as a unit) by identifying it with the hook name. Some names refer to filters, others to hooks.

Themes and plugins can define additional hook names. This lets developers open their plugins up to other developers in a controlled and safe way. Popular plugins like WooCommerce define custom hooks, letting independent developers add features or tweaks with their own plugins.

Briefly, an action directs WordPress to do something at a particular point in the execution of its code, and a filter modifies a particular kind of data.

Filters

A filter is a function which takes some data and returns it in a modified form. It could return completely different data, or even a null value to indicate the data should be discarded. Filters can affect any part of a site, including post content, page content, titles, tags, links, lists of posts and pages, and much more. They can modify the administrative pages as well as the public content.

Filters don’t apply just to displayed text, but to data read from and written to the WordPress database and to email notifications.

As a whimsical example, a filter could replace every occurrence of the word “duck” in comments with a duck emoji. More usefully, filters can do things like these:

  • Specifying the formats of dates and timestamps.
  • Blocking forbidden words or links.
  • Specifying the subject and content of email notifications to the administrator.
  • Controlling a theme’s appearance in ways that CSS can’t do.

Actions

An action is a PHP function that WordPress invokes at a specified point in its execution. Multiple actions can be added to the same hook name; each one has a priority value that controls the order of execution. Some points where actions can be hooked include:

FREE PERFORMANCE CHECKLIST Your site performance checklist to help you assess your website health   

  • After WordPress is loaded.
  • When a user logs in.
  • When WordPress creates a profile for a user.
  • When a user’s profile is updated.
  • When a post is published.

An action can serve many purposes, such as these:

  • Adding security features.
  • Modifying other data in response to a user action.
  • Providing warnings or notifications.
  • Restricting what a user can do.

What it Means For You

WordPress users, other than developers, don’t see actions and filters directly. You install themes and plugins; hooks are part of them, but only WordPress coders work with them. Knowing the basics of actions and filters does help to understand what plugins and themes can do, though.

The most popular plugins often have a network of independent developers associated with them. Even a plugin that offers rich functionality can’t do everything that a customer might want. Developers can create their own plugins for niche markets, adding specialized capabilities. It’s even possible to create a plugin for a specific customer, modifying the plugin to meet specific needs.

Without hooks, developers would have to fit their add-on code to whatever points in the plugin looked amenable to modification. This is a risky approach, since the code might change in the next release. Hooks let a plugin offer an API which doesn’t need to change when the code changes. The add-ons should remain stable through new releases of the plugin.

Knowing a little about filters and actions helps you to understand the versatility that WordPress themes and plugins offer. Almost everything in a site can be customized, including not just what’s presented but how users and administrators interact with it.

Newsletter Signup

Share