Академический Документы
Профессиональный Документы
Культура Документы
1
Hooks?
2
WordPress Hooks
– WordPress Codex
3
Once more!
4
But it’s more than that!
Can be used anywhere
Themes OR Plugins
Note
In case of a theme, can be used in functions.php or a file included in
functions.php for better code organization
5
Be Like Budh
This is Budh
Budh works with WordPress
He doesn’t write spaghetti code
He uses WordPress Hooks
Be like Budh
6
A World without Hooks
Y U No Use Hooks?
8
Types of WordPress Hooks
Filters Actions
9
Filters and Actions
Filters Actions
Applying changing to content Hook your functions into an action
// The Hook // The Hook
a p p l y f i l t e r s () ; do action () ;
// T i e i n t o t h e Hook // T i e i n t o t h e Hook
a d d f i l t e r () ; add action () ;
10
But they sound so similar?
11
Yes they are ... except!
Filters and actions are almost the same thing in syntax and capabilities
but:
Caution
“With great power comes great responsibility” – Not Uncle Ben
12
Filters Actions
• Tying into existing WordPress
• Change existing WordPress
processes, like creating a new
content, blog posts, text, media
user, saving a post, updating
files etc. Like cropping text or
permalinks, sending emails,
modifying a word.
saving a comment, etc.
• Change database content on
• Adding an action to your plugin
the fly or display it differently
/ theme to allow other
• Clean up poorly formatted code
developers to manipulate it
b ‘filtering out’ certain tags
without hacking
13
Makes a little more sense now!
14
Code Demos
15
f u n c t i o n e x a m p l e c a l l b a c k ( $example ) {
// Maybe m o d i f y $ e x a m p l e i n some way .
r e t u r n $example ;
}
16
// i f t h e page i s c a l l e d ’ debug ’
function cleanup ( $content ) {
return s t r i p t a g s ( $content ) ;
}
17
function publish post tweet ( $post ID ) {
g l o b a l $post ;
// Code t o s e n d a t w e e t w i t h p o s t i n f o
}
18
function create my widget () {
register sidebar ( array (
’ name ’ => ( ’My S i d e b a r ’ , ’ mytheme ’ ) ,
’ i d ’ => ’ m y s i d e b a r ’ ,
’ d e s c r i p t i o n ’ => ( ’ The one and o n l y ’ , ’ mytheme ’ ) ,
));
}
19
// H e l p s t e s t i n g t h e e m a i l f u n c t i o n a l i t y
function testEmail () {
w p m a i l ( ’ i n f o @ f a h d m u r t a z a . com ’ , ’ T e s t s o f t w a r e ’ , ’ T e s t i n g
some e m a i l a t ’ . g e t b l o g i n f o ( ’ u r l ’ ) ) ;
}
20
Questions?
“For everything you know, there are many
more people out there that don’t know.”
Don’t be shy!
info@fahdmurtaza.com
(@fahdmurtaza)
https://goo.gl/xb5deC
20