Understanding WordPress Action Hooks

The moment you start digging the tiniest bit under the surface of WordPress in your attempt to either tweak your website or create a theme or plugin, you will come face-to-face with WordPress action hooks. The common question among people who are going deeper with WordPress is what are action hooks and how do I use them? So this post will answer those two questions, plus give you a boost up in your WordPress development pursuits.

What are WordPress Action Hooks?

At the very basic level, action hooks are placeholders. They are specific locations in your WordPress theme that allow functions/code/etc to hook into. The most basic examples of a action hooks, that normally exists in every single WordPress site, are the wp_head and wp_footer action hooks. Both of these are placeholders that are located at… (you guessed it)… inside the header of the theme and in the footer of the theme. Typical uses of these two action hooks are to place analytic tracking codes or to add some additional CSS files to a WordPress page.

In a nutshell, an action hook is a predefined place on your site left open by the developer of the site where the user/developer can insert specific code to expand the capability of the site.

How do you use an Action Hook?

Let’s use an example of adding some additional meta tag descriptions to the section of our WordPress site. Because an action hook is just a placeholder, we need to create a function, a piece of code that will run in that action hook location. Our sample function will add a meta tag description to our WordPress site.

function bb_actionhook_example() {
  echo '<meta name="description" content="My new meta description." />' "n";
}

This function is then hooked into the action hook by using the WordPress add_action() function. (More information about add_action() can be found on the WordPress Codex.)

add_action('wp_head', 'bb_actionhook_example');

Now our site contains our new meta description tag because we’ve run the function in the wp_head action hook location.

What if my theme doesn’t come with any action hooks?

Most WordPress themes today have many action hooks to enhance the flexibility and capability of the theme. Four popular and powerful themes, Catalyst, Builder, Genesis, and Startbox have a wide assortment of action hooks to give developers a lot of power in customizing the look and functionality of a site. But what if your theme doesn’t have the amount of action hook you want? Or what if your theme doesn’t have action hooks in the right place? This is an easy fix, because you can add your own action hooks to any WordPress theme.

If you open up your theme’s template files (PHP files) you should be able to locate the location where you desire an action hook to reside. At that location, simple use this line of PHP code to create a new action hook.

<?php do_action('my_new_action_hook'); ?>

Now you can use the newly created my_new_action_hook to hook in your own functions and expand your current theme dynamically.

How do you use Action Hooks?

Side Note: If you want a list of all default action hooks in WordPress, visit this page on the Codex.

    1 Ping:
  1. Pingback: The 8th WordPress Developer Course is Coming, July 29 – Aug 3, 2013 :: WebDesign.com

Leave a Reply

Your email address will not be published. Required fields are marked *

*