5 W's of Hookin'
Nowell VanHoesen
@NowellVanHoesen
about.me/NowellVanHoesen
Hookin' Overview
What are Hooks
Who hooks
Why hook
Where are they
When to hook
Examples
@NowellVanHoesen5 W's of Hookin'
What are hooks?
A specific place/time in WordPress code
execution to add functionality or change data.
@NowellVanHoesen5 W...
What are hooks?
A specific place/time in WordPress code
execution to add functionality or change data.
Types:
– Action: ad...
What are hooks?
A specific place/time in WordPress code
execution to add functionality or change data.
Types:
– Action: ad...
Who hooks?
@NowellVanHoesen5 W's of Hookin'
Who hooks?
Any one who wants to add custom code to
change how WordPress behaves or modify
what is output to the browser.
@...
Who hooks?
Any one who wants to add custom code to
change how WordPress behaves or modify
what is output to the browser.
A...
Why
@NowellVanHoesen5 W's of Hookin'
Why
use hooks
@NowellVanHoesen5 W's of Hookin'
Why
use hooks
● Tweak
functionality/output
to better fit your
needs
@NowellVanHoesen5 W's of Hookin'
Why
use hooks
● Tweak
functionality/output
to better fit your
needs
● Maintain your
customizations when
updating
@NowellVa...
Why
use hooks
● Tweak
functionality/output
to better fit your
needs
● Maintain your
customizations when
updating
create ho...
Why
use hooks
● Tweak
functionality/output
to better fit your
needs
● Maintain your
customizations when
updating
create ho...
Why
use hooks
● Tweak
functionality/output
to better fit your
needs
● Maintain your
customizations when
updating
create ho...
Where...
can I find them
@NowellVanHoesen5 W's of Hookin'
Where...
can I find them
● WordPress core
@NowellVanHoesen5 W's of Hookin'
Where...
can I find them
● WordPress core
● Themes
@NowellVanHoesen5 W's of Hookin'
Where...
can I find them
● WordPress core
● Themes
● Plugins
@NowellVanHoesen5 W's of Hookin'
Where...
can I find them
● WordPress core
● Themes
● Plugins
can I create them
@NowellVanHoesen5 W's of Hookin'
Where...
can I find them
● WordPress core
● Themes
● Plugins
can I create them
● Themes
@NowellVanHoesen5 W's of Hookin'
Where...
can I find them
● WordPress core
● Themes
● Plugins
can I create them
● Themes
● Plugins
@NowellVanHoesen5 W's of...
Where...
can I find them
● WordPress core
● Themes
● Plugins
can I create them
● Themes
● Plugins
● WordPress core
@Nowell...
Where...
can I find them
● WordPress core
● Themes
● Plugins
can I create them
● Themes
● Plugins
● WordPress core
@Nowell...
How?
Actions Filters
@NowellVanHoesen5 W's of Hookin'
How?
Actions
● add_action( t, f, p, a )
Filters
● add_filter( t, f, p, a )
@NowellVanHoesen5 W's of Hookin'
t = tag f = fu...
How?
Actions
● add_action( t, f, p, a )
● remove_action( t, f,
p, a )
Filters
● add_filter( t, f, p, a )
● remove_filter( ...
How?
Actions
● add_action( t, f, p, a )
● remove_action( t, f,
p, a )
● do_action( t, f, p, a )
Filters
● add_filter( t, f...
How?
Actions
● add_action( t, f, p, a )
● remove_action( t, f,
p, a )
● do_action( t, f, p, a )
Filters
● add_filter( t, f...
Action hook example 1
// wp_enqueue_scripts action
add_action( 'wp_enqueue_scripts', 'nv_added_styles' );
function nv_adde...
Action hook example 2
add_action( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
global $wp_the_quer...
Filter hook examples
add_filter( 'the_title', 'nv_the_title', 10, 2 );
function nv_the_title( $title, $id ) {
if ( !is_adm...
Time to go live...
@NowellVanHoesen5 W's of Hookin'
Time to go live...
@NowellVanHoesen5 W's of Hookin'
Resources
@NowellVanHoesen5 W's of Hookin'
Plugin API on the codex
- http://codex.wordpress.org/Plugin_API
Drew Jaynes – F...
Upcoming SlideShare
Loading in …5
×

5 W's of Hookin'

390 views

Published on

The Who, What, Where, When, and Why of using WordPress Hooks. We will explore examples of rolling our own to allow other developers to hook into our code.

Published in: Technology, Business
0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total views
390
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
3
Comments
0
Likes
0
Embeds 0
No embeds

No notes for slide

5 W's of Hookin'

  1. 1. 5 W's of Hookin' Nowell VanHoesen @NowellVanHoesen about.me/NowellVanHoesen
  2. 2. Hookin' Overview What are Hooks Who hooks Why hook Where are they When to hook Examples @NowellVanHoesen5 W's of Hookin'
  3. 3. What are hooks? A specific place/time in WordPress code execution to add functionality or change data. @NowellVanHoesen5 W's of Hookin'
  4. 4. What are hooks? A specific place/time in WordPress code execution to add functionality or change data. Types: – Action: add functionality at a specific point when an event happens or is about to happen. @NowellVanHoesen5 W's of Hookin'
  5. 5. What are hooks? A specific place/time in WordPress code execution to add functionality or change data. Types: – Action: add functionality at a specific point when an event happens or is about to happen. – Filter: modify data before some event ( save, display ) @NowellVanHoesen5 W's of Hookin'
  6. 6. Who hooks? @NowellVanHoesen5 W's of Hookin'
  7. 7. Who hooks? Any one who wants to add custom code to change how WordPress behaves or modify what is output to the browser. @NowellVanHoesen5 W's of Hookin'
  8. 8. Who hooks? Any one who wants to add custom code to change how WordPress behaves or modify what is output to the browser. Anyone who wants to allow others to build on to their plugin or theme. @NowellVanHoesen5 W's of Hookin'
  9. 9. Why @NowellVanHoesen5 W's of Hookin'
  10. 10. Why use hooks @NowellVanHoesen5 W's of Hookin'
  11. 11. Why use hooks ● Tweak functionality/output to better fit your needs @NowellVanHoesen5 W's of Hookin'
  12. 12. Why use hooks ● Tweak functionality/output to better fit your needs ● Maintain your customizations when updating @NowellVanHoesen5 W's of Hookin'
  13. 13. Why use hooks ● Tweak functionality/output to better fit your needs ● Maintain your customizations when updating create hooks @NowellVanHoesen5 W's of Hookin'
  14. 14. Why use hooks ● Tweak functionality/output to better fit your needs ● Maintain your customizations when updating create hooks ● Allow others to add functionality @NowellVanHoesen5 W's of Hookin'
  15. 15. Why use hooks ● Tweak functionality/output to better fit your needs ● Maintain your customizations when updating create hooks ● Allow others to add functionality ● Allow others to filter output from your plugin/theme @NowellVanHoesen5 W's of Hookin'
  16. 16. Where... can I find them @NowellVanHoesen5 W's of Hookin'
  17. 17. Where... can I find them ● WordPress core @NowellVanHoesen5 W's of Hookin'
  18. 18. Where... can I find them ● WordPress core ● Themes @NowellVanHoesen5 W's of Hookin'
  19. 19. Where... can I find them ● WordPress core ● Themes ● Plugins @NowellVanHoesen5 W's of Hookin'
  20. 20. Where... can I find them ● WordPress core ● Themes ● Plugins can I create them @NowellVanHoesen5 W's of Hookin'
  21. 21. Where... can I find them ● WordPress core ● Themes ● Plugins can I create them ● Themes @NowellVanHoesen5 W's of Hookin'
  22. 22. Where... can I find them ● WordPress core ● Themes ● Plugins can I create them ● Themes ● Plugins @NowellVanHoesen5 W's of Hookin'
  23. 23. Where... can I find them ● WordPress core ● Themes ● Plugins can I create them ● Themes ● Plugins ● WordPress core @NowellVanHoesen5 W's of Hookin'
  24. 24. Where... can I find them ● WordPress core ● Themes ● Plugins can I create them ● Themes ● Plugins ● WordPress core @NowellVanHoesen5 W's of Hookin'
  25. 25. How? Actions Filters @NowellVanHoesen5 W's of Hookin'
  26. 26. How? Actions ● add_action( t, f, p, a ) Filters ● add_filter( t, f, p, a ) @NowellVanHoesen5 W's of Hookin' t = tag f = function p = priority a = # args
  27. 27. How? Actions ● add_action( t, f, p, a ) ● remove_action( t, f, p, a ) Filters ● add_filter( t, f, p, a ) ● remove_filter( t, f, p, a ) @NowellVanHoesen5 W's of Hookin' t = tag f = function p = priority a = # args
  28. 28. How? Actions ● add_action( t, f, p, a ) ● remove_action( t, f, p, a ) ● do_action( t, f, p, a ) Filters ● add_filter( t, f, p, a ) ● remove_filter( t, f, p, a ) ● apply_filters( t, f, p, a ) @NowellVanHoesen5 W's of Hookin' t = tag f = function p = priority a = # args
  29. 29. How? Actions ● add_action( t, f, p, a ) ● remove_action( t, f, p, a ) ● do_action( t, f, p, a ) Filters ● add_filter( t, f, p, a ) ● remove_filter( t, f, p, a ) ● apply_filters( t, f, p, a ) @NowellVanHoesen5 W's of Hookin' http://codex.wordpress.org/Plugin_API t = tag f = function p = priority a = # args
  30. 30. Action hook example 1 // wp_enqueue_scripts action add_action( 'wp_enqueue_scripts', 'nv_added_styles' ); function nv_added_styles() { if ( !is_admin() ) { wp_register_style( 'nv-ie-fix', get_bloginfo( 'stylesheet_directory' ) . '/ie.css', false ); $GLOBALS['wp_styles']->add_data( 'nv-ie-fix', 'conditional', 'lte IE 8' ); wp_enqueue_style( 'nv-ie-fix' ); } } @NowellVanHoesen5 W's of Hookin'
  31. 31. Action hook example 2 add_action( 'pre_get_posts', 'my_get_posts' ); function my_get_posts( $query ) { global $wp_the_query; if( $wp_the_query === $query && $query->is_home() ) { add_filter( 'posts_where' , 'posts_where' ); $category_id = get_cat_ID( 'Scribblings' ); $query->set( 'cat', $category_id ); } else if ( is_archive() && is_date() && !is_category()) { if ( !isset( $query->query_vars['post_type'] ) ) { $query->set( 'post_type', array( 'post', 'myoldhouse', 'snapshots' ) ); $query->set( 'posts_per_page', -1 ); } } } function posts_where( $where ) { $where = " AND ( ( wp_term_relationships.term_taxonomy_id IN (1) AND wp_posts.post_type = 'post' ) OR ( wp_posts.post_type IN ('myoldhouse', 'snapshots') ) ) AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')"; return $where; } @NowellVanHoesen5 W's of Hookin'
  32. 32. Filter hook examples add_filter( 'the_title', 'nv_the_title', 10, 2 ); function nv_the_title( $title, $id ) { if ( !is_admin() ) { $title = 'WCCbus - ' . $title; } return $title; } // what core runs return apply_filters( 'the_title', $title, $id ); add_filter( 'enter_title_here', 'change_default_post_title', 10, 2 ); function change_default_post_title( $text, $post ) { if ( 'post' == get_post_type( $post ) ) { return $text . “ - Required”; } } @NowellVanHoesen5 W's of Hookin'
  33. 33. Time to go live... @NowellVanHoesen5 W's of Hookin'
  34. 34. Time to go live... @NowellVanHoesen5 W's of Hookin'
  35. 35. Resources @NowellVanHoesen5 W's of Hookin' Plugin API on the codex - http://codex.wordpress.org/Plugin_API Drew Jaynes – Filter of the Day: Three filters a day for 365 days - http://fotd.werdswords.com/

×