Thanks to hooks, you can add your cool plugin functionality to the various parts of WordPress, like wp_head() or wp_footer(), which are both useful for a lot of things. The Web statistics example in the previous article, where you added a function in the theme’s functions. php template file and then added it to the wp_footer() hook using add_action(), is very much the way of the plugin.
More often than not, you’ll end up writing a function in your plugin, and then you’ll add it to one of the hooks. This is how add_action() works:
add_action ('hook-name', 'function-name', X, Y );
The 'hook-name' is of course the name of the hook you want to add the action to. What happens is that WordPress, when encountering that hook when running the code, will check to see if there are any added functions registered for that hook in particular. If there are, the function will run; if not, it won’t. And the function is, of course, the one you’ve defined in 'function-name'.
X and Y are optional integers. The first, X, is the priority argument (which defaults to 10), used to sort actions when there are several added to a specific action hook. The lower the number is, the earlier the function will be executed, so if you need to make sure that something happens before or after something else, this is where you control that.
Y, on the other hand, is the number of arguments your function can accept (defaulting to 1). If you need the function to handle more than one argument, you can set it by entering another number here. Most of the time you won’t see the priority or number of arguments in plugins, but sometimes they can come in handy. As they are optional, you can just leave them out when you don’t need them. To sum it up, this code snippet would cause the 'smashing-shortcode' function to run when wp_head() is executed:
add_action ('wp_head', 'smashing-shortcode');
Filters work more or less the same way, but you use add_filter() rather than add_action(). The parameters are the same, and you pass them the same way as well. The only difference is that you can’t use the action hooks with add_filter(); you use the filter hooks instead. Other than that the two act in the same way.
Moving on, there are two types of hooks. Action hooks are triggered by specific events when a WordPress site is running, such as publishing a post, which would be the publish_post() hook. Filter hooks, on the other hand, are functions that WordPress passes data through, so you’d use them to do stuffwith the data. Useful filter hooks include the_excerpt(), the_title(), and so on. You ought to recognize them from the template tags.
Adding functions to a hook, whether it is on of the action or filter kind, is easy enough, but what about removing functionality? Sometimes you don’t want an action or filter hook to run, and that means you need to make sure it gets removed. This is done with remove_action() and remove_filter() for action and filter hooks, respectively. The syntax is simple:
remove_action('action-hook','the-function')
And the same for remove_filter(). This is not just used to remove functionality you have added, you can also remove core functionality from within WordPress with these. Any action or filter function can be removed this way, from pinging to deleting attachments, so some plugins may actually consist only of limiting WordPress functionality, rather than extending it, all depending on what your goal is.
Our website is not responsible for the information contained by this article. Articleinput.com is a free articles resource thus practically any visitor can submit an article. However if you notice any copyrighted material, please contact us and we will remove the article(s) in discussion right away.
Note: This article was sent to us by: Eliott Anister at 05212010
1. How to do lifestreaming with WordPress
All articles are property of their respective authors. Please read our Privacy Policy!
© 2009 ArticleInput.com.
Partners: Damenmode