WordPress Hooks and Filters

Presentation by Nick Batik, @Nick_Batik

Nick’s slides are available at http://presentations.handsonwp.com/category/wordpress-hooks-and-filters/

The WordPress page lifecycle

  • The events from when a web page is requested to when the page is returned to the browser
  • Various information is communicated to and from the server where the website is housed, including a user’s browser and cookie information, and the WP function is called, and the template loader is fired, ending with the page appearing in the browser
  • There are hundreds of files that are called in the construction of a page, and happens every time a visitor clicks on your site, and most happen within fractions of a second.
  • There are points in here where we can effect change.

What are hooks and how do they fit into the page lifecycle?

“Hooks enable us to literally hook into parts of the WordPress page lifecycle to retrieve, insert, or modify data, or they allow us to take certain actions behind the scenes.”

– Tom McFarlin @TUTSPLUS

  • Think of hooks like mailboxes, and you just need to find the correct mailbox to insert information.
  • Almost all of Genesis framework is built around adding and removing things from hooks and filters.
  • Technically, hooks are a description for two different types of things: actions and filters.
  • Action add or modify a sequence of events. Examples: removing comments; create landing page.
  • Filters are used to change data within your site. For example, you can filter that look for comments with profanity, or find and correct misspellings of your company name.

Actions and filters examples

There are actions to perform, add or remove actions from a page.

Action Hooks

  • do_action()
  • add_action()
  • remove_action()
  • has_action()
  • do_action_ref_array()
  • did_action()
  • remove_all_actions()

Using Action Hooks

  • do_action( ‘hook_name’, [$arg] );
  • add_action ( ‘hook_name’, ‘your_function_name’, [priority], [accepted_args] );
  • remove_action( ‘hook_name’, ‘your_function_name’, [$priority], [$accepted_args]);


  • apply_filters()
  • add_filter()
  • remove_filter()
  • has_filter()
  • current_filter()
  • merge_filters()
  • remove_all_filters()

Using Filters

  • apply_filters( ‘hook_name’, $value, [$var …] );
  • add_filter( ‘hook_name’, ‘function_to_ add’, [$priority], [$accepted_args] );
  • remove_filter( ‘hook_name’, ‘function_ to_remove’, [$priority], [$accepted_args ] );

A simple filter

add_filter(‘the_content’, ‘my_filter_function’, 10);

function my_filter_function($content) {
$output = '<div>'.$content.'</div>';
return $output;

More on hooks and filters

  • Hook provide places to either the operation of WordPress or the data it’s working on. Hook names, or tags, are how you identify what you’re going to be working on (the mailboxes). Hooks and actions are designed to modify what is happening in WordPress without having to modify the core installation of WordPress itself.
  • Many themes and plugins add their own hooks and filters to WordPress as well.
  • There’s an array, $wp_filter, that stores tags and associated filter or action functions.

Where can you learn what hooks and filters are out there?


Using hooks and filters sometimes comes from trial and error. Whether the results of a hook shows up in the post editor or on the final page depends on how the hook is written.

For those that requested, her is the link to last month’s presentation by Chris Weigman: https://speakerdeck.com/chriswiegman/php-an-introduction-to-the-language-beneath-wordpress

%d bloggers like this: