Understanding WordPress Filters and Actions

Post on 23-Aug-2014

306 Views

Category:

Internet

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

Understanding WordPress Filters & Actions on a basic level, get started with some simple notes and samples.

Transcript

build create W O R D C A M P C H I C A G O

W O R D P R E S S F I LT E R S & A C T I O N S

U N D E R S TA N D I N G

build create W O R D C A M P C H I C A G O

build create W O R D C A M P C H I C A G O

H I .

Designer, developer, marketer, shit-talker, etc.

Owner build/create studios

@buildcreate @wilsonographybuildcreate.com

I ’ M I A N , N I C E T O M E E T Y O U .

build create W O R D C A M P C H I C A G O

M Y S E L F I S H G O A L S F O R Y O U

I want you to walk away from this feeling a little less hacky, a little more informed, and most of all inspired.

I want you to have an “Ah ha!” moment, where suddenly your understanding of WordPress becomes that much clearer, and you can’t wait to get out of this room to try out your new knowledge.

Here goes nothing…

build create W O R D C A M P C H I C A G O

A W O R L D W I T H O U T H O O K S

build create W O R D C A M P C H I C A G O

I T ’ S P R E T T Y D A M N H A C K Y

• Including extra PHP files for ajax requests

• Hacking into plugins to change display/functionality

• Hacking *gasp* WP core files

• Hacking theme files (when you should be using a child theme)

• Hackity hack hack hack.

build create W O R D C A M P C H I C A G O

L E T ’ S TA L K A B O U T H O O K S

“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.”

- T O M M C FA R L I N @ T U T S P L U S

build create W O R D C A M P C H I C A G O

Where does all of this fit into the WP loading process?

build create W O R D C A M P C H I C A G O

S O T H E Y ’ R E B O T H H O O K S , T H E Y C A N K E E P M E F R O M H A C K I N G M Y W O R D P R E S S I N S TA L L T O S H R E D S W I T H M Y I N E P T I T U D E , I G E T T H AT, W H AT ’ S T H E D I F F E R E N C E ?

build create W O R D C A M P C H I C A G O

F I LT E R S

• Applying changes to content/data

• Uses functions:

• apply_filters()

• add_filters()

A C T I O N S

• Hooks your functions into an action in WP

• Uses functions:

• do_action()

• add_action()

build create W O R D C A M P C H I C A G O

F I LT E R S

• Change something you’re pulling out of WP

• Change something you’re putting into the WP database

A C T I O N S

• Using WP Ajax

• Tying into existing WP processes, like sending email

• Adding styles or js to the page

W H E N S H O U L D W E U S E T H E S E L O V E LY H O O K S ?

build create W O R D C A M P C H I C A G O

A K A A L L T H E D A M N T I M E . I T ’ S F R E A K I N G M A G I C P E O P L E .

build create W O R D C A M P C H I C A G O

S O , L E T S M A K E S O M E M A G I C …

build create W O R D C A M P C H I C A G O

I N Y O U R T E M P L A T E F I L E

I N Y O U R F U N C T I O N S F I L E

L E T S M A K E A N A C T I O N !

build create W O R D C A M P C H I C A G O

I N Y O U R F U N C T I O N S F I L E

L E T S F I LT E R T H E C O N T E N T !

( Y E A H I T ’ S T H AT S I M P L E , A N D T H I S I S L I K E 9 0 % O F W H AT I U S E F I LT E R S F O R )

build create W O R D C A M P C H I C A G O

L E T S G E T D A N G E R O U S

build create W O R D C A M P C H I C A G O

A D D I N G D A N C I N G O L D P E O P L E T O T H E PA G E I S F U N , B U T H O W A B O U T S O M E T H I N G Y O U C A N A C T U A L LY U S E …

Adding our JS to the footer with the wp_footer hook

Tying into the WP Ajax hooks and creating our callback

build create W O R D C A M P C H I C A G O

L E T S M A K E O U R O W N F I LT E R , S T Y L E A N A R R AY, T H E N A P P LY A N O T H E R F I LT E R T O T H AT F I LT E R , T H E N S P I T I T A L L O U T O N T H E PA G E …

Filters, not just for strings any more. Let’s format the hell out of this array!

Take note of the priority difference between the filters!

Now in our page template we can feed some content into the filter and get the result back.

build create W O R D C A M P C H I C A G O

W A N T T O A D D A S E A R C H F O R M T O Y O U R M A I N N AV ? T H E R E ’ S A H O O K F O R T H AT.

We get the menu HTML from WordPress, and simply append our search form and return the result.

build create W O R D C A M P C H I C A G O

M E O W W E ’ R E TA L K I N !

build create W O R D C A M P C H I C A G O

N O W Y O U ’ R E T H I N K I N G W I T H H O O K S

• Magical WP-friendly ajax requests!

• Work WITH plugins as the author intended instead of hacking them!

• GTFO WP core files!

• Build better themes and child themes!

• ????

• Profit!

build create W O R D C A M P C H I C A G O

T H A N K S F O R C O M I N G !

• http://codex.wordpress.org/Plugin_API/Action_Reference

• http://code.tutsplus.com/articles/the-beginners-guide-to-wordpress-actions-and-filters--wp-27373

• http://www.zell-weekeat.com/wordpress-actions-and-filters/

• http://codex.wordpress.org/Plugin_API/Filter_Reference

R E S O U R C E S

build create W O R D C A M P C H I C A G O

top related