• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Hooks WCSD12
 

Hooks WCSD12

on

  • 5,147 views

Basic to intermediate skills for WordPress hooks (actions and filters).

Basic to intermediate skills for WordPress hooks (actions and filters).

Statistics

Views

Total Views
5,147
Views on SlideShare
4,266
Embed Views
881

Actions

Likes
6
Downloads
115
Comments
0

13 Embeds 881

http://weblog.mediatemple.net 485
http://mediatemple.net 248
http://172.17.5.167 66
http://cloud.feedly.com 50
http://www.newsblur.com 10
http://feedly.com 7
http://lanyrd.com 4
http://digg.com 3
http://54a.8b6.myftpupload.com 3
http://reader.aol.com 2
http://ull.mn 1
http://www.slashdocs.com 1
http://newsblur.com 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Hooks WCSD12 Hooks WCSD12 Presentation Transcript

    • WordPress Hooks Actions and Filters WordCamp San Diego 2012
    • Jeffrey Zinnocean goer, wordpress fanboy,avid backpacker, euro gamer,soccer hooligan,voracious coffee drinker• co-founder of pixel jar• wordcamp oc co-organizer• adsanity co-developer• @jeffreyzinn• jeff@jzinn.us
    • What are Hooks?• Actions - Actions are the hooks that the WordPress core launches at specific points during execution, or when specific events occur.Your plugin can specify that one or more of its PHP functions are executed at these points, using the Action API.• Filters - Filters are the hooks that WordPress launches to modify text of various types before adding it to the database or sending it to the browser screen.Your plugin can specify that one or more of its PHP functions is executed to modify specific types of text at these times, using the Filter API.
    • Wait...What are Hooks?•Actions - Do Stuff• Filters - Change Stuff
    • Why Hooks?• Crack into code without editing core files• Let others alter your work
    • HypotheticalLet’s say we were desperate for a widget to place inour sidebar that displays the word “awesome.”
    • Basic Awesome Widgetactivate
    • Basic Awesome Widget sidebaractivate
    • Basic Awesome Widget sidebaractivate test
    • HypotheticalUpon further consideration you realize this widget issorely lacking in puppy pictures. Now what? Let’slook in the code...
    • Basic Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = $instance[title]; echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; echo <h2>awesome</h2>; echo $after_widget;}
    • Basic Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = $instance[title]; echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; echo <h2>awesome</h2>; echo $after_widget;}
    • Deluxe Awesome Widget twi dge sam e
    • Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( before_awesome_widget ); echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); do_action( after_awesome_widget ); echo $after_widget;}
    • Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( before_awesome_widget ); echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); do_action( after_awesome_widget ); echo $after_widget;}
    • Deluxe Widget Code public function widget( $args, $instance ) { extract( $args );filter! $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( before_awesome_widget ); echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); do_action( after_awesome_widget ); echo $after_widget; }
    • Deluxe Widget Code public function widget( $args, $instance ) { extract( $args );filter! $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; action! do_action( before_awesome_widget ); echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); do_action( after_awesome_widget ); echo $after_widget; }
    • Deluxe Widget Code public function widget( $args, $instance ) { extract( $args );filter! $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; action! do_action( before_awesome_widget ); filter! echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); do_action( after_awesome_widget ); echo $after_widget; }
    • Deluxe Widget Code public function widget( $args, $instance ) { extract( $args );filter! $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; action! do_action( before_awesome_widget ); filter! echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); action! do_action( after_awesome_widget ); echo $after_widget; }
    • Actions (do stuff)• do_action() - Creates a hook for attaching actions via add_action()• add_action() - Hooks a function onto a specific action created with do_action()• remove_action() - Removes a function attached to a specified action hook
    • Actions: do_action() do_action( $tag, $arg, $extra_arg );• $tag - (string) (required) The name of the hook you wish to create. Default: None• $arg - (mixed) (optional) The list of arguments this hook accepts. Default: ...their code...
    • Actions: add_action()add_action( $tag, $function_to_add, $priority, $accepted_args );• $tag - (string) (required) The name of the action you wish to hook onto. Default: None• $function_to_add - (callback) (required) The name of the function you wish to be called. Default: None• $priority - (int) (optional) How important your function is. Alter this to make your function be called before or after other functions. Default: 10• $accepted_args - (int) (optional) How many arguments your function takes. Default: 1 ...our code...
    • Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( before_awesome_widget ); echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); do_action( after_awesome_widget ); echo $after_widget;}
    • Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( before_awesome_widget ); echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); do_action( after_awesome_widget ); echo $after_widget;}
    • Deluxe Widget Code public function widget( $args, $instance ) { extract( $args ); $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; action! do_action( before_awesome_widget ); echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); do_action( after_awesome_widget ); echo $after_widget; }
    • do_action( before_awesome_widget );which action? which function of ours?add_action( ‘before_awesome_widget’, ‘puppy’ );function puppy() { $src = http://placedog.com/g/188/150; echo <img src=" . $src . " />;} ...in your functions.php script....
    • do_action( before_awesome_widget );which action? which function of ours?add_action( ‘before_awesome_widget’, ‘puppy’ );function puppy() { $src = http://placedog.com/g/188/150; echo <img src=" . $src . " />;} ...in your functions.php script....
    • do_action( before_awesome_widget );which action? which function of ours?add_action( ‘before_awesome_widget’, ‘puppy’ );function puppy() { $src = http://placedog.com/g/188/150; echo <img src=" . $src . " />;} Use a unique function name ...in your functions.php script....
    • Multiple Calls to Actionadd_action( ‘before_awesome_widget’, ‘puppy’ );function puppy() { $src = http://placedog.com/g/188/150; echo <img src=" . $src . " />;}add_action( ‘before_awesome_widget’, ‘warning’ );function warning() { echo <p>Warning: puppies!</p>;} ...in your functions.php script....
    • Multiple Calls to Actionadd_action( ‘before_awesome_widget’, ‘puppy’ );function puppy() { $src = http://placedog.com/g/188/150; Same echo <img src=" . $src . " />; Action}add_action( ‘before_awesome_widget’, ‘warning’ );function warning() { echo <p>Warning: puppies!</p>;} ...in your functions.php script....
    • Multiple Calls to Actionadd_action( ‘before_awesome_widget’, ‘puppy’ );function puppy() { $src = http://placedog.com/g/188/150; Diff. F Same echo <img src=" . $src . " />; unctio Action} nsadd_action( ‘before_awesome_widget’, ‘warning’ );function warning() { echo <p>Warning: puppies!</p>;} ...in your functions.php script....
    • Multiple Calls to Action
    • Multiple Calls to ActionAction: before_awesome_widgetFunction: puppy
    • Multiple Calls to ActionAction: before_awesome_widgetFunction: puppyAction: before_awesome_widgetFunction: warning
    • Ordering Actionsadd_action( ‘before_awesome_widget’, ‘puppy’, 2 );function puppy() { $src = http://placedog.com/g/188/150; echo <img src=" . $src . " />; $priority (default 10)}add_action( ‘before_awesome_widget’, ‘warning’, 1 );function warning() { echo <p>Warning: puppies!</p>;} ...in your functions.php script....
    • Ordering Actions
    • Ordering ActionsAction: before_awesome_widgetFunction: warningPriority: 1
    • Ordering ActionsAction: before_awesome_widgetFunction: warningPriority: 1Action: before_awesome_widgetFunction: puppyPriority: 2
    • Actions: remove_action()remove_action( $tag, $function_to_remove, $priority, $accepted_args ); remove_action( before_awesome_widget, warning, 1 );
    • Actions: remove_action()remove_action( $tag, $function_to_remove, $priority, $accepted_args ); remove_action( before_awesome_widget, warning, 1 ); Be as specific as the original action
    • Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( before_awesome_widget ); echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); do_action( after_awesome_widget ); echo $after_widget;}
    • Deluxe Widget Code public function widget( $args, $instance ) { extract( $args ); $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( before_awesome_widget ); filter! echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); do_action( after_awesome_widget ); echo $after_widget; }
    • Filters (change text)• apply_filters() - Calls the functions added to a filter hook via add_filter()• add_filter() - Hooks a function onto a specific filter action created with add_filters()• remove_filter() - Removes a function attached to a specified filter action hook
    • Filters: apply_filters() apply_filters( $tag, $value, $var ... );• $tag - (string) (required) The name of the filter hook. Default: None• $value - (mixed) (required) The value which the filters hooked to $tag may modify. Default: None• $var - (mixed) (optional) One or more additional variables passed to the filter functions. Default: None
    • Filters: add_filter()add_filter( $tag, $function_to_add, $priority, $accepted_args ); • $tag - (string) (required) The name of the filter to hook the $function_to_add to. Default: None • $function_to_add - (callback) (required) The name of the function to be called when the filter is applied. Default: None • $priority - (int) (optional) Used to specify the order in which the functions associated with a particular action are executed. Functions with the same priority are executed in the order in which they were added to the action. Default: 10 • $accepted_args - (int) (optional) Number of arguments the function(s) accept (s). Default: 1
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> ); which filter? which function of ours?add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;} ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;} Use a unique function name ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;} ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;} always return! ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;} always return! ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;}add_filter( ‘filter_awesome_text’, ‘second_filter’ );function second_filter( $text ) { $text = ‘<h2>dogs!</h2>’; return $text;} ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;}add_filter( ‘filter_awesome_text’, ‘second_filter’ );function second_filter( $text ) { $text = ‘<h2>dogs!</h2>’; return $text;} ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;}add_filter( ‘filter_awesome_text’, ‘second_filter’ );function second_filter( $text ) { $text = ‘<h2>dogs!</h2>’; return $text;} ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;}add_filter( ‘filter_awesome_text’, ‘second_filter’ );function second_filter( $text ) { $text = ‘<h2>dogs!</h2>’; return $text;} ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;}add_filter( ‘filter_awesome_text’, ‘second_filter’ );function second_filter( $text ) { $text = ‘<h2>dogs!</h2>’; return $text;} ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’, 2 );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;}add_filter( ‘filter_awesome_text’, ‘second_filter’, 1 );function second_filter( $text ) { $text = ‘<h2>dogs!</h2>’; return $text;} ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’, 2 );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;}add_filter( ‘filter_awesome_text’, ‘second_filter’, 1 );function second_filter( $text ) { $text = ‘<h2>dogs!</h2>’; return $text;} ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’, 2 );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;}add_filter( ‘filter_awesome_text’, ‘second_filter’, 1 );function second_filter( $text ) { $text = ‘<h2>dogs!</h2>’; return $text;} ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’, 2 );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;}add_filter( ‘filter_awesome_text’, ‘second_filter’, 1 );function second_filter( $text ) { $text = ‘<h2>dogs!</h2>’; return $text;} ...in your functions.php script....
    • echo apply_filters( filter_awesome_text, <h2>awesome</h2> );add_filter( ‘filter_awesome_text’, ‘filter_awesome’, 2 );function filter_awesome( $text ) { $text .= ‘<h4>puppies</h4>’; return $text;}add_filter( ‘filter_awesome_text’, ‘second_filter’, 1 );function second_filter( $text ) { $text = ‘<h2>dogs!</h2>’; return $text;} ...in your functions.php script....
    • remove_filter()remove_filter( $tag, $function_to_remove, $priority, $accepted_args ); remove_filter( the_content, wpautop );
    • Ridiculous Filterpublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( before_ridiculous_widget ); $wrap = ( is_home() ) ? h2 : h3; $text = awesome; $pattern = <%s class="awesome">%s</%s>; $awesome = sprintf( $pattern, $wrap, $text, $wrap ); echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text ); do_action( after_ridiculous_widget ); echo $after_widget;}
    • Ridiculous Filter public function widget( $args, $instance ) { extract( $args ); $title = apply_filters( widget_title, $instance[title] ); echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( before_ridiculous_widget ); $wrap = ( is_home() ) ? h2 : h3; $text = awesome; $pattern = <%s class="awesome">%s</%s>; $awesome = sprintf( $pattern, $wrap, $text, $wrap ); filter! echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text ); do_action( after_ridiculous_widget ); echo $after_widget; }
    • Ridiculous Filter$wrap = ( is_home() ) ? h2 : h3;$text = awesome;$pattern = <%s>%s</%s>;$awesome = sprintf( $pattern, $wrap, $text, $wrap ); echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text );
    • Ridiculous Filter$wrap = ( is_home() ) ? h2 : h3;$text = awesome;$pattern = <%s>%s</%s>;$awesome = sprintf( $pattern, $wrap, $text, $wrap ); echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text ); is_home() !is_home() <h2>awesome</h2> <h3>awesome</h3>
    • Ridiculous Filter$wrap = ( is_home() ) ? h2 : h3;$text = awesome;$pattern = <%s>%s</%s>; ??????$awesome = sprintf( $pattern, $wrap, $text, $wrap ); echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text ); is_home() !is_home() <h2>awesome</h2> <h3>awesome</h3>
    • echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text );...assume is_home()... add_filter( filter_ridiculous_text, ridiculous_filter, 10, 3 ); function ridiculous_filter( $text, $var1, $var2 ) { $var2 = "not awesome"; $new = sprintf( <%s>%s</%s>, $var1, $var2, $var1 ); return $new; } ...in your functions.php script....
    • echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text );...assume is_home()... add_filter( filter_ridiculous_text, ridiculous_filter, 10, 3 ); function ridiculous_filter( $text, $var1, $var2 ) { $var2 = "not awesome"; $new = sprintf( <%s>%s</%s>, $var1, $var2, $var1 ); return $new; } ...in your functions.php script....
    • echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text );...assume is_home()... add_filter( filter_ridiculous_text, ridiculous_filter, 10, 3 ); function ridiculous_filter( $text, $var1, $var2 ) { $var2 = "not awesome"; $new = sprintf( <%s>%s</%s>, $var1, $var2, $var1 ); return $new; } ...in your functions.php script....
    • echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text );...assume is_home()... <h2>awesome</h2> add_filter( filter_ridiculous_text, ridiculous_filter, 10, 3 ); function ridiculous_filter( $text, $var1, $var2 ) { $var2 = "not awesome"; $new = sprintf( <%s>%s</%s>, $var1, $var2, $var1 ); return $new; } ...in your functions.php script....
    • echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text );...assume is_home()... <h2>awesome</h2> h2 add_filter( filter_ridiculous_text, ridiculous_filter, 10, 3 ); function ridiculous_filter( $text, $var1, $var2 ) { $var2 = "not awesome"; $new = sprintf( <%s>%s</%s>, $var1, $var2, $var1 ); return $new; } ...in your functions.php script....
    • echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text );...assume is_home()... <h2>awesome</h2> h2 awesome add_filter( filter_ridiculous_text, ridiculous_filter, 10, 3 ); function ridiculous_filter( $text, $var1, $var2 ) { $var2 = "not awesome"; $new = sprintf( <%s>%s</%s>, $var1, $var2, $var1 ); return $new; } ...in your functions.php script....
    • echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text );...assume is_home()... add_filter( filter_ridiculous_text, ridiculous_filter, 10, 3 ); function ridiculous_filter( $text, $var1, $var2 ) { $var2 = "not awesome"; $new = sprintf( <%s>%s</%s>, $var1, $var2, $var1 ); return $new; } ...in your functions.php script....
    • echo apply_filters( filter_ridiculous_text, $awesome, $wrap, $text );...assume is_home()... add_filter( filter_ridiculous_text, ridiculous_filter, 10, 3 ); function ridiculous_filter( $text, $var1, $var2 ) { $var2 = "not awesome"; $new = sprintf( <%s>%s</%s>, $var1, $var2, $var1 ); return $new; } ...in your functions.php script....
    • Examples
    • Genesis Framework
    • Genesis Actions
    • Genesis Filters
    • Other Themes and Plugins?• WordPress: 790+ actions, 1250+ filters• Genesis Framework: 180+ actions; 100+ filters• Gravity Forms: 120+ actions; 270+ filters• Shopp: 130+ actions; 230+ filters
    • Where Hooks?• Plugin/Theme Wiki• Documentation• Actual Code (search for do_action and apply_filters)• Google search!• http://codex.wordpress.org/Plugin_API
    • Yes, Hooks!•Actions - Do Stuff• Filters - Change Stuff
    • do_action( ‘the_end’ ) add_action( ‘the_end’, ‘questions’ ); function questions() { echo ‘Are there any questions?’; }slides: http://slideshare.net/jeffreyzinn/hooks-wcsd12 code: https://gist.github.com/2163780