Inevitably the average WordPress user is going to come face to face with a WordPress action or filter hook in order to complete the customization that they are looking for.

Action Hook Overview

There are two types of hooks in WordPress and we will cover the more complicated and confusing filter hook. Action hooks are the other type and are used primarily for inserting content into template areas that may be otherwise inaccessible. For example from a plugin, you want to inject some content into the footer of your page. You would do something like this, which is directly from the codex:

function your_function() {
echo 'This is inserted at the bottom';
}
add_action('wp_footer', 'your_function');

In the above code we are using the add_action call to specify which hook–or “location” for lack of a better term–is going be used when we call our function in the second argument. ‘your_function’ could easily be ‘my_function’ or ‘silly_nilly’ as long as it matches the name of the actual function you are defining to do something in the specified location.

The add_action call can also come before or after the function definition, it does not matter.
You test this code by placing it in the functions.php file of your current theme.

The Mighty Filter Hook

I find that it can be frustrating having to use a filter hook when all I want is to insert some content somewhere. This is because filter hooks are a bit trickier and must return a value. There is a very good explanation on stack exchange  between the two, The difference between filter and action hooks

Since the point of this post is to show beginners how to use a filter hook we are only going to skim the surface of what is possible, in this case we are going to treat it like an action hook and simply insert some content into a template area.

In the PageLines framework we can reference filters here: PageLines API Hooks

Now since there is no action hook that can be applied to the area just after the post metabar we will use a filter instead to add content.

add_filter('pagelines_post_metabar', 'my_metabar_addition');

function my_metabar_addition($metabar){
$metabar .= 'Added Content';
return $metabar;
}

The above code takes a custom function and adds to the variable which contains the output of the metabar. If we had used

function my_metabar_addition($metabar){
$metabar = 'Added Content';
return $metabar;
}

Our metabar would simply say “Added Content” because we would have reset the variable $metabar instead of adding to what output already exists in the core files. By using the dot before the equal sign we concatenate the two values.

2 Responses to WordPress Filter Hooks: A Beginner’s Guide

  1. Tindel says:

    Love your work on pagelines and am trying to get better. Any suggestions on what I need to learn to make your lesson clearer on hooks. I get what they do. Just not how easily all that code was created to make it do that. I can edit any code already written, but am just learning to write it. Thanks for your help.

  2. cmunns says:

    You will definitely want to get used to basic PHP syntax if you want to write your own code in WordPress/PageLines without pulling your hair out. Missing semi-colons and brackets which will make your screen go white can be really tricky when you are first starting.

    Hooks/filters are inserted into templates and frameworks specifically to allow editing of content later in a more controlled fashion and without having to copy over whole section files into a child theme.

    Here is some more reading, not sure if it’s going to help clarify or confuse you more but give it a shot

    http://wp.smashingmagazine.com/2011/10/07/definitive-guide-wordpress-hooks/

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop us a note so we can take care of it!

Archives

All entries, chronologically...