Hooks allow code to interact with and modify other code at certain points in the WordPress page lifecycle. There are two main types of hooks: actions, which execute code when events occur; and filters, which modify data before it is sent to the browser or database. Actions are used when you want something to happen, like sending an email when a post is published. Filters are used when you want to change something, like adding text to post titles. Hooks are constructed by specifying a tag name, callback function, priority, and arguments.
1 of 25
Download to read offline
More Related Content
WordPress Hooks (Actions & Filters)
1. What the Hook
What YOU should know about
WordPress Actions & Filters
M u h a m m a d K a s h i f
m e @ K a s h i f . i o
2. Intro
Muhammad Kashif
F o u n d e r & C E O - M e s h P r o s
M a s t e r T r a i n e r - e R o z g a a r
O r g a n i z e r W C L a h o r e 2 0 1 9
6. What areHooks?
Hooks enable us to literally hook into parts of
the WordPress page lifecycle to retrieve, insert,
or modify data, or they allow us to take certain
actions behind the scenes.
-TOM MCFARLIN @ TUTSPLUS
9. Actions
Functions that are executed when
something happens in WordPress.
For example:
When a post is published
Before the header is loaded
Declared with add_action
10. Actions
Functions that are executed when
something happens in WordPress.
You can attach custom code at theseevents:
11. Actions
Functions that are executed when
something happens in WordPress.
You can attach custom code at theseevents:
When a post is published, send me anemail
12. Actions
Functions that are executed when
something happens in WordPress.
You can attach custom code at theseevents:
When a post is published, send me anemail
When the header is loaded, add some css
13. Functions that WordPress passes
data through before it is sent tothe
browser or database.
Filters
14. Functions that WordPress passes
data through before it is sent tothe
browser or database.
For example:
When the post title isdisplayed
When the author url isoutput
Declared with add_filter
Filters
15. Filters
Functions that WordPress passes
data through before it is sent tothe
browser or database.
You can attach custom code at theseevents:
16. Filters
Functions that WordPress passes
data through before it is sent tothe
browser or database.
You can attach custom code at theseevents:
When the post title is displayed, add something
21. How to construct a hook
tag - the name of the action to attach code to
your_function - the name of your custom function
$priority -the order that the functions are executed
$args - the number of arguments sent to your function