Select Page

One of the most powerful features in WordPress is also one of the most feared – hooks.

This article explains what they are and gives you two examples of how you might use them to customise your WordPress-based website.

When you redecorate a room you might go through these stages:

  1. Strip the old wallpaper
  2. Fill any holes in the plaster
  3. Sand down any lumpy bits on the wall
  4. Put the new wallpaper on the wall

WordPress goes through a similar process when preparing a post to display:

  1. Load the plugins
  2. Setup the theme
  3. Parse the HTTP request
  4. Get the posts to display
  5. Prepare the header
  6. Add the post(s)
  7. Add the footer

Each of these stages (and I’ve stripped them down to their most basic) allows you to add your own code to be executed by triggering any actions or filters that you might have attached to them.

These bits of code are called “hooks” because they hook on to the core code at different stages and they enable you to completely customise how your installation of WordPress behaves.

There are two types of hooks available to you: action hooks and filter hooks.

Action hooks enable you to perform actions (processing) when triggered.

Filter hooks enable you to filter the content that WordPress has retrieved.

Action hooks

Action hooks enable you to run your own PHP functions at specific stages of the page rendering process. An example might be inserting a random selection of posts at the end of the post being displayed. In short – when WordPress reaches certain points in page generation it could call your functions before continuing.

Filter hooks

Filter hooks enable you to manipulate the content that’s just been created. For example, if the post content had just been retrieved, you could a filter it so that all instances of the word, ‘kitten’ became underlined. In short – when wordpress has created the content it can pass it through your function to alter it.

Note – a filter hook must be used with pre-existing content (for example the post content). You can add to the content, remove stuff from the content or format it in funky ways but everything you do must be done to the content.

Adding the time and date the page was generated at the bottom of the page

This example uses the wp_footer action hook to call my PHP function. Just pop it in the functions.php file of your theme. The wp_footer hook is triggered when the page is almost completely rendered. I’m going to use it to insert the current date and time at the end of the page. A word of warning though – not all themes support the wp_footer hook (but most do).

function actionhook_addtimestampcomment ($post_object) {
     // Add the time and date at the bottom of the body
     echo ''; 
     echo 'Page generated at '; 
     echo date('l, dS F, Y \a\t H:i:s'); 
     echo ''; 
 add_action( 'wp_footer', 'actionhook_addtimestampcomment' );

Using a filter hook to highlight the word kitten in yellow

Just above I mentioned that filter hooks enable you to change the content of data – in this case I’m going to change the post content so that any instances of the word ‘kitten’ are highlighted with a yellow background. Again, this code needs to go in the functions.php file in your theme’s folder.

function filterhook_highlightkitten ( $content ) {
     $content = str_replace('kitten','<span style="background-color: #ffff00;">kitten</span>',$content);
     return $content;
 add_filter( 'the_content', 'filterhook_highlightkitten' );

Wrapping up

In this article I’ve introduced the concept of WordPress hooks – actions that enable you to trigger your own functions when WordPress does certain things and filters that enable you to change the content that WordPress displays.

Where to find out more

Needless to say, unless you have a list of the hooks that are available, this information isn’t going to be that much use. Fortunately the Internet has some great sources of information to help you find the hook you’re looking for.