Вы находитесь на странице: 1из 22

WordPress Hooks 101

Fahad Murtaza (@fahdmurtaza)


Dec, 2018
WordCamp Biratnagar, 2018
Testing 123 ... attention please!

• Setting the Objectives


• Walk away from this, feeling a little inspired and less hacky.
• Less spaghetti in your code.
• “Eureka” moments, where suddenly your understand it and want to
try it out
• Let’s have fun!

1
Hooks?

2
WordPress Hooks

Hooks are provided by WordPress to allow your plugin to ’hook


into’ the rest of WordPress; that is, to call functions in your
plugin at specific times, and thereby set your plugin in motion.

– WordPress Codex

3
Once more!

“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 McFlarin @ Tutsplus

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

• Including extra PHP files for


things like AJAX requests
• Including files in the header /
footer
• Hacking into plugins to change
display/functionality while you
should be using a . . . yes . . . a
hook
• Hacking WP core files..
Seriously, who does that?
• Hacking theme files (when you
should be using a child theme).
With custom theme an
exception to this rule.
7
So... Raju

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:

• The difference is how you use them!


• Both are powerful but remember:

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

• Sending all email notifications from gravity forms when a form is


submitted
• Send an email on a post update
• Send an email on a post create / publish
• Reset the permalinks on a new custom post type registration
• Filtering the bad words from content
• Resizing the image

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 ;
}

a d d f i l t e r ( ’ example filter ’ , ’ example callback ’ ) ;

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 ) ;
}

a d d f i l t e r ( ’ the content ’ , ’ cleanup ’ ) ;

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
}

add action ( ’ publish post ’ , ’ publish post tweet ’ ) ;

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 ’ ) ,
));
}

add action ( ’ w i d g e t s i n i t ’ , ’ create my widget ’ ) ;

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 ’ ) ) ;
}

add action ( ’ i n i t ’ , ’ testEmail ’ ) ;

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

Вам также может понравиться