Everything you need to know about WordPress hooks

Everything you need to know about WordPress hooks

Abbey Fitzgerald's Layout avatar

Hooked on WordPress hooks?

There’s a lot going on under the hood as WordPress works to render the needed component for your website. Each page is made up of quite a few functions and database queries. The WordPress core and the theme work together to output text, images, stylesheets, and other files. With the help of the browser, all of these pieces are interpreted and put together into one web page. Hooks give the ability to customize, extend, and enhance WordPress.

Hooks are named appropriately because we can literally “hook into” WordPress to retrieve, insert, or modify data, or do other tasks behind the scenes. In a sense, we “hang” our custom code on those hooks. As we know, modifying the WordPress core is not a good idea. Because of this, action hooks and filter hooks in WordPress are the best way to change existing or create new functionality.

Types of hooks

Hooks are a necessary part when making customizations. There are two primary types of hooks: action hooks and filter hooks. Each offers something different, so it is important to understand when to use which one.

A hook can’t be simply thrown in just anywhere; there needs to be something to “hook into.” Having that point of execution for that hook is necessary. The good news is that throughout the WordPress core, there are built-in hooks that are available to reference.

Four emergency rope

Why use hooks

A lot can be changed with hooks because many of WordPress’s core functions use actions and filters. Understanding hooks is absolutely necessary for anyone developing with WordPress and for designers that want to modify WordPress or the theme’s behavior.

Action Hooks

Action hooks indicate that something has happened, not to be confused with events themselves. Actions are triggered by specific events that take place in WordPress. An example of an event could be things like publishing a post, changing themes, or activating a plugin. Actions allow you to add extra functionality at a specific point in the processing of the page.  Action hooks can do things like:

  • Modify data in the WordPress database
  • Modify what is displayed in the browser
  • Send an email when an event has happened
  • Add a widget, menu, or a custom message to the page

wordpress-hooks-action

Action Hook Example

add_action( $hook, $function_to_add, $priority, $accepted_args );

The required parameters of the add_action function are the hook and function to add. Including the priority is optional. With an integer value based on a scale of 1 to 999, this number determines the priority order of functions for that specific hook. You may not need it, but the last parameter is used when you need to pass or accept multiple arguments.

Let’s take a look at a basic action hook:

// This is the function, name it accordingly

function custom_welcome_text() { ?>

<div class="optional-custom-class">Hello WordPress!</div>

<?php }

&nbsp;

// Action function that outputs the function above into the theme hook

add_action( 'welcome_hook', 'custom_welcome_text', 5 );

This basic hook will add “Hello WordPress” to the top of the page. It has a priority of 5. Lower numbers correspond with earlier execution. By default it is set to 10, so this example executes before the default.

Filter Hooks

Let’s go over a common scenario and how filter hooks come into play. Think of all the times that your users visit your site and a post loads for them to read. As part of the WordPress page lifecycle, WordPress queries the database for that post, then it is returned to the browser, ready to for the user to read. It all sounds simple, but before the content is served, WordPress runs the data through any filters that have been established. These filter hooks can easily be created when needed.

Think of it this way: actions are triggered by specific events, but filters allow for the interception and modification of data as it is processed. Filter hooks are used when you need to intercept, manage, or return data before rendering it to the browser or saving data from the browser to the database. For example, you might want to insert another CSS class in a WordPress HTML element, or register additional meta field types. Both of these can be executed with a filter hook.

wordpress-hooks-filter

Filters sit between the database and the browser as WordPress generates pages. Also, they sit between the browser and database as WordPress adds new posts and comments to the database. Because of this, filter hooks can do helpful things like:

  • Add data to the database or send to the browser screen
  • Manipulate data coming out of the database before it goes into the browser or coming from the browser prior to going into the database.

Filter hook example

In the example below, we’ll look at how a filter hook works on content. This hook is fired just before content will be used, put in the database, or rendered by WordPress for onscreen viewing. This alters the data when the filter hook is fired.

The add_filter function is what we need to hook into a filter. The arguments may look familiar to you, the add_filter is the same as that of add_action. Let’s now hook into the WordPress filter the_content, which is called before any post content is displayed. In this simple example, the filter will allow us to add a copyright note to every post. At this point, the filters will take action on data that’s being passed to them.

add_filter( 'the_content', 'add_copyright_notice' );

function add_copyright_notice( $content )

{

return $content . " <br>This content is copyrighted.";

}

Please note, when you use a filter, you must always return something or things will break.

Adding hooks to a theme

When adding hooks and filters to the theme, be sure that you are working in a child theme. The main reason is that if you are working in the parent theme, these modifications could be overwritten if there is an update to the theme files.

Hopefully you have a better understanding of hooks, what they can do, and how they can help. Hooks can be very simple, very complex, or somewhere in between. Once you have the basic concepts, you will be hooked in no time.

Comments ( 0 )

Join the discussion