This content originally appeared on Zell Liew and was authored by Zell Liew
Wordpress action and filter hooks are what makes Wordpress incredibly extendable. These hooks are very easy to use if someone else has already wrote them, and you just had to hook something in. Understanding how they work is another matter entirely.
In this post, I’m going to walk you through my personal understanding of Wordpress action and filer hooks after a few hours of experiments.
The Difference Between Actions and Filters
The main difference between actions and filters is the purpose they are used for and how they are declared and used. Here’s a quick summary.
- Actions
When something has to be added
declared with
add_action()
. used withdo_action()
. - Filters
When something has to be changed
declared with
apply_filters()
. used withadd_filters().
Not sure you get what I mean? Lets dive into some examples of creating and using actions and filters.
Creating Wordpress Actions
You can create a wordpress action with the following code.
php add_action('my_action_hook_name', 'my_action_function_name', $priority); function my_action_function_name() { // things your function should do } // Note: $priority is optional, and defaults to 10 Multiple functions to a single action hook, allowing many possibities of adding functionality to a particular area. When the above code is executed, Wordpress searches the actions list for`'my_action_hook_name'`. If `my_action_hook_name` is not found, Wordpress creates the action hook. If `my_action_hook_name` is already declared, Wordpress tries to find out when it should execute the function with the `$priority `. The lower a number given to priority, the earlier that particular code will execute. Since you now know how to create an Wordpress action, the next step is to know how to use it. ## Using Wordpress Actions You can you the wordpress action with the following code.This content originally appeared on Zell Liew and was authored by Zell Liew
Zell Liew | Sciencx (2014-02-10T00:00:00+00:00) Understanding WordPress Actions and Filters. Retrieved from https://www.scien.cx/2014/02/10/understanding-wordpress-actions-and-filters/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.