Bearded man looking something up in a large book

What Are WordPress Hooks?

WordPress is known for its power and customizability, and themes make that power easier to access for most users. However, themes are limited by the functionality developers build into them. Finding one that does exactly what you need isn’t always easy.

That’s where hooks come in. WordPress hooks are a powerful way to quickly add almost any function to your site. They can either perform an action (actions) or change something (filters) on your site. The best part is their flexibility. If there’s something you want to do, there’s probably a hook that can help.

In this article, we’ll cover the basics of hooks, actions, and filters, and then walk you through some examples. Let’s get started!

Key Terms Related to Hooks

It’s helpful to have a little background before we dive into how to create your own hooks. There are a few common terms that you’ll frequently run across when dealing with hooks: actions, filters, wp_head, and the_content.

Actions and filters are the two basic functions performed by hooks, while wp_head and the_content are two of the most commonly-used hooks for actions and filters, respectively.

Action

An action is a function performed when a given event occurs in WordPress. In other words, actions do something. Some common examples of actions include:

  • Sending an email or other notification to users when a new post is published
  • Loading a widget in the footer of a page
  • Displaying an instruction box above a login form

Actions are already in use on nearly every WordPress site, although they’re typically hidden behind the Graphical User Interface (GUI) of themes and plugins. You’ll only really see them if you intentionally dig into the source code to look for them – or if you write your own code.

Filter

If an action is a do hook, a filter is a change hook. This type of hook enables you to modify another function. Common examples of filters in action include:

  • Capitalizing the words in a post’s title automatically
  • Attaching links to related posts at the end of a piece of content
  • Pulling up posts from a specific tag or category

Like actions, you’re probably using filters all over your website already, without even knowing it.

wp_head

The wp_head action hook triggers an action in the header of your page – inside the <head></head> section of the theme’s code. This is important because numerous critical functions are located within the header in WordPress. Some examples include Google Analytics tracking codes and the noindex action, which tells search engines to not include a page in search results. 

the_content

Next up, the_content is a filter that applies to the content of a post. It’s used to apply basic formatting – a common example is adding paragraph tags and line breaks into a post’s code automatically. Another frequent use of the_content filters is displaying social sharing buttons at the bottom of a post.

How Do I Create a Hook in WordPress?

Now that you understand the basics of hooks, it’s time to create one of your own. To do that, you need to know that hooks are made up of three basic parts: 

  • The hook itself, which tells WordPress where to perform the function
  • An action or filter, which tells WordPress what function you want to perform
  • A callback to a function in the WordPress library, which tells WordPress how to carry out the action or filter

For the most part, the hook, action, and filter will already be defined within either the WordPress core files or your theme’s files. This means you’ll just need to connect the two, and depending on the function you want to execute, add some parameters.

For the latter, you can consult the relevant page on the WordPress Codex. Let’s see what all of this looks like in action with a couple of examples.

Example of an Action Hook

To illustrate an action hook, we’ll use wp_head and the noindex function. This is a fairly common function that tells search engine crawlers to skip indexing a page, and it’s typically run from the page’s header section. 

The wp_head hook is found in your theme’s header.php file:

Image of where to find the wp_head hook in your WordPress theme's header

The action, noindex, is found in wp-includes/general-template.php. This file is part of WordPress core, and contains the following code:

function noindex() {
// If the blog is not public, tell robots to go away.
if ( '0' == get_option( 'blog_public' ) ) {
wp_no_robots()
   }
}

You’ll need to connect these two with a callback in the functions.php file of your theme. It’s best to add this to a child theme rather than to your main theme, because it may otherwise get overwritten when the theme updates.

Open up your functions.php file in your WordPress dashboard (Appearance > Theme Editor), and add the following code to the bottom of the file:

add_action( 'wp_head', 'noindex' );

Now, when the header loads it will perform the noindex function, which ‘tells robots to go away’. 

Example of a Filter Hook

For our filter example, we’ll use the_content and wpautop. This function automatically adds HTML tags for your paragraphs and includes line breaks between them, so they’re properly displayed in the browser.

The hook, the_content, is found in the wp-includes/post-template.php file. This is the relevant code:

apply_filters( 'the_content', string $content )

The wpautop function is found in the wp-includes/formatting.php file. Here’s what it looks like:

function wpautop( $pee, $br = true ) {
 // …
return $pee;
}

Here’s the code you need to add to functions.php in order to connect these two:

add_filter( 'the_content', 'wpautop' );

Once you’ve added the code and saved the file, your new hook should start doing its job right away.

Where Can I Find a List of All WordPress Hooks?

There are an incredible number of hooks built into WordPress. Memorizing all of them is a challenge, even for seasoned developers. Fortunately, there are some excellent references online that list all of the available hooks. The Action Reference and Filter Reference on the WordPress Codex are perfect starting points.

Explore WP Engine’s Developer Resources

Hooks in WordPress enable you to quickly customize your site. Plus, you don’t have to be a pro web developer to use them. Even a coding newbie can learn to implement hooks in WordPress with a little practice. 

No matter what type of WordPress user you are, WP Engine offers in-depth developer resources to help you provide an incredible digital experience for your users. Be sure to check them out!

Get started

Build faster, protect your brand, and grow your business with a WordPress platform built to power remarkable online experiences.