Understanding WordPress Actions and Filters

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 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 with do_action().
  • Filters When something has to be changed declared with apply_filters(). used with add_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


Print Share Comment Cite Upload Translate
APA
Zell Liew | Sciencx (2022-12-01T20:20:44+00:00) » Understanding WordPress Actions and Filters. Retrieved from https://www.scien.cx/2014/02/10/understanding-wordpress-actions-and-filters/.
MLA
" » Understanding WordPress Actions and Filters." Zell Liew | Sciencx - Monday February 10, 2014, https://www.scien.cx/2014/02/10/understanding-wordpress-actions-and-filters/
HARVARD
Zell Liew | Sciencx Monday February 10, 2014 » Understanding WordPress Actions and Filters., viewed 2022-12-01T20:20:44+00:00,<https://www.scien.cx/2014/02/10/understanding-wordpress-actions-and-filters/>
VANCOUVER
Zell Liew | Sciencx - » Understanding WordPress Actions and Filters. [Internet]. [Accessed 2022-12-01T20:20:44+00:00]. Available from: https://www.scien.cx/2014/02/10/understanding-wordpress-actions-and-filters/
CHICAGO
" » Understanding WordPress Actions and Filters." Zell Liew | Sciencx - Accessed 2022-12-01T20:20:44+00:00. https://www.scien.cx/2014/02/10/understanding-wordpress-actions-and-filters/
IEEE
" » Understanding WordPress Actions and Filters." Zell Liew | Sciencx [Online]. Available: https://www.scien.cx/2014/02/10/understanding-wordpress-actions-and-filters/. [Accessed: 2022-12-01T20:20:44+00:00]
rf:citation
» Understanding WordPress Actions and Filters | Zell Liew | Sciencx | https://www.scien.cx/2014/02/10/understanding-wordpress-actions-and-filters/ | 2022-12-01T20:20:44+00:00
https://github.com/addpipe/simple-recorderjs-demo