sammela.com header image 2

Amazing Wordpress Hooks

October 12th, 2009 · No Comments · Uncategorized

In thirty years of programming I have seen a lot of what were called “frameworks”.

Some frameworks were too complicated and abstract to use, some were poorly documented, some weren’t very well written; but in my mind the WordPress framework is superb, and a key feature of the WordPress framework is “hooks”.

What are Hooks

So just what exactly are WordPress hooks?

This is how the WordPress documentation defines hooks:

Hooks are provided by WordPress to allow your plugin to ‘hook into’ the rest of WordPress; that is, to call functions in your plugin at specific times, and thereby set your plugin in motion.

This is true, but let’s try to look at it another way.

Let’s say you have a guy who comes and fertilizes your lawn twice a year — once in the spring and once in the fall.  You decide you’d like him to water your lawn after he fertilizes.  You call him up and you say, “Hey, from now on when you fertilize my lawn in the spring, please water it afterwards; and when you fertilize my lawn in the fall, please water it .”

That’s it. You just hooked the activity of lawn watering into spring fertilizing and fall fertilizing.  Note that you don’t need to know if spring fertilizing was on March 17 or April 7.  It doesn’t matter.  Same with fall fertilizing.  If the lawn guy follows your instructions he just waters after he fertilizes.   Furthermore, if you have a reliable lawn guy, you don’t need to pay attention to when he fertilizes and then remind him to water, because one activity follows another.  It’s scheduled.

Tying One Activity to Another

Simply put, hooks tie one activity to another in WordPress.  There are actually two types of hooks defined in WordPress — Actions and Filters.  We’ll get into that more later in this article, but for now let us look at an example of the use of a hook.  We’ll use the example given in the WordPress documentation.

Let us suppose that you want an  E-mail sent to a friend of your friends John and Maureen every time you make a post. Here’s a handy function to do that:

1
2
3
4
5
6
7
function email_post_info($post_ID)  
{
    $addresses = "john@sam_example.org,maureen@sam_example.org";
    mail( $addresses, "Sam blog entry",
      "Please see blog entry: http://blog.sam_example.com");
    return $post_ID;
}

Good function, but how does it get called?

Simple — attach the email_post_info function to the publish_post action; and fortunately WordPress offers a simple way to do that, shown below:

1
add_action ( "publish_post", "email_post_info");

Basically, that’s it. The WordPress add_action function above is used to register the email_post_info function to be called when WordPress executes the publish_post action.

Not that “publish_post” is one of many standard hook names defined in WordPress. For example, other post related actions are “comment_post”, “deleted_post”, “edit_post”. There are various compilations of these actions at Plugin API/Hooks 2.0.x , The WordPress Hooks Database, and Comprehensive List for Plugin and Theme Developers. The latter claims to included “documented as well as undocumented” hooks.

Action Hooks Versus Filter Hooks

Action hooks are designed to allow Wordpress to call a plugin function when specific events occur in WordPress. In other words, they cause an action to take place.

Filter hooks, on the other hand, are designed to make changes in specific types of data. In other words, they filter data.

We already looked at an action hook in the previous section. Now let’s look at a filter hook.

Filter Hook Example

Here is a simple example of a WordPress filter hook, taken from the WordPress default-filters.php file:

1
file add_filter('the_title', 'trim');

What does this do?

It applies the standard PHP Library trim function to the page title.

http://www.dinke.net/blog/2007/08/06/php-callback-functions-and-oop/en/

References

- Understanding Action Hooks In Wordpress
- Plugin API
- Writing a Plugin
- Plugin API/Hooks 2.0.x

Tags:

No Comments so far ↓

There are no comments yet...Kick things off by filling out the form below.

Leave a Comment