Chris Farrell Membership
Store Coach
HobbyTron
This site uses cookies. By continuing to browse the site, you are agreeing to our use of cookies. Find out more.

130 WordPress Hooks, Actions and Filters (How-to Video)

 Posted by  Add comments
 

130 WordPress Hooks, Actions and Filters (Video)

Part of the “How to Write a WordPress Plugin” series

This Lesson – Overview

In previous lessons we introduced plugins, built a basic working plugin template file structure and created a plugin class for our plugin.

In this lesson we’ll cover hooks, filters and actions.

Contents

What are Hooks, Actions and Filters?

What are Hooks, Action and Filters? Unfortunately, the more research I do, the more I realise people simply don’t understand the concept of hooks, actions and filters very well. Most people seem to find this concept the biggest barrier to creating plugins.

Just remember as we continue, plugins, hooks, actions and filters are nothing more than php files containing functions which interact with WordPress and each other.

Hooks

  • Hooks are simply placeholders in your php template files.

At various times during the execution of the WordPress code, WordPress checks to see if any plugins have registered functions to run at that time, and if so, those functions are run.

WordPress provides lots of ‘hooks’ for plugins to hook into their code.

According to WordPress:

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. There are two kinds of hooks: Actions and Filters.

For a full list of hooks see, http://codex.wordpress.org/Plugin_API/Hooks_2.0.x

Why Hooks are Necessary

Like any major piece of software, WordPress continuously evolves. Without hooks, if you wanted to change or extend some functionality of WordPress you would have to modify core WordPress files every time a new version of WordPress was release.

Thanks to hooks, you can upgrade, both WordPress and your plugin, knowing that your coding is separate and will continue to operate as specified.

To use hooks you only need to do two things:

  • Create a functon to implement your code.
  • Create a call at the hook position to your code.

That might sound strange but once you see how easy WordPress has made this task, you’ll be both supprised and amaized.

What are Actions?

WordPress states:

Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur. Your plugin can specify that one or more of its PHP functions are executed at these points, using the Action API.

Actions are points in the execution of a page or process lifecycle that WordPress fires.

The basic steps to making this happen are:

  • Create a PHP function to execute when the event occurs.
  • Hook the function to the event by calling add_action()

From the WordPress Plugin API page, the add_action call is as follows:

What are Filters?

WordPress states:

Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen. Your plugin can specify that one or more of its PHP functions is executed to modify specific types of text at these times, using the Filter API.

  • Filters are points of execution in which WordPress modifies data before saving it or sending it to the browser.

The basic steps to making this happen are:

  • Create a PHP function to execute when the event occurs.
  • Hook the function to the event by calling add_filter()

From the WordPress Function Reference page, the add_filter call is as follows:

add_filter ( ‘hook_name’, ‘your_function_name’, [priority], [accepted_args] );

Which to Use? Action or Filter!

You can sometimes accomplish the same goal with either an action or a filter. For example, if you want your plugin to change the text of a post, you might add an action function to publish_post, so the post is modified as it is saved to the database, or a filter function to the_content, so the post is modified as it is displayed in the browser screen.

  • The key to your plugin is determining what event should trigger your plugin.

Providing Your Own Hooks

Not only does WordPress provide you with a LOT of hooks to use, you can even create your own, so allowing others to develop plugins to enhance your code.

The method is couldn’t be simpler, just enter;

<?php do_action( ‘your-action-hook-name’ ); ?>

Do remember to make your hook name something unique.

Conclusion – Summary

Well, that’s all for the tutorial. Hopefully this lesson gave you some insight into hooks, action and filters. I’ll see you in the next lessons when we will start writing some useful action and filter functions.


KingSolutions.org.uk is hosted on JustHost

  One Response to “130 WordPress Hooks, Actions and Filters (How-to Video)”

  1. Great tutorials, thank you ;)

 Leave a Reply

(required)

(required)

91 queries in 0.956 seconds (Child).