WordPress Hooks
   Actions and Filters
    WordCamp San Diego 2012
Jeffrey Zinn
ocean 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
Hypothetical
Let’s say we were desperate for a widget to place in
our sidebar that displays the word “awesome.”
Basic Awesome Widget

activate
Basic Awesome Widget
               sidebar


activate
Basic Awesome Widget
                  sidebar


activate




           test
Hypothetical
Upon further consideration you realize this widget is
sorely lacking in puppy pictures. Now what? Let’s
look in the code...
Basic Widget Code
public 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 Code
public 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

       t
wi dge
              sam
                  e
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' );
	

   	

	

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

   	

	

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

   	

	

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

   	

	

   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....
Hooks WCSD12
Multiple Calls to Action
add_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 Action
add_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 Action
add_action( ‘before_awesome_widget’, ‘puppy’ );
function puppy() {
   $src = 'http://placedog.com/g/188/150';




                                         Diff. F
                      Same
   echo '<img src="' . $src . '" />';




                                           unctio
                       Action
}




                                                 ns
add_action( ‘before_awesome_widget’, ‘warning’ );
function warning() {
   echo '<p>Warning: puppies!</p>';
}

        ...in your functions.php script....
Multiple Calls to Action
Multiple Calls to Action
Action: before_awesome_widget
Function: puppy
Multiple Calls to Action
Action: before_awesome_widget
Function: puppy


Action: before_awesome_widget
Function: warning
Ordering Actions
add_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 Actions
Action: before_awesome_widget
Function: warning
Priority: 1
Ordering Actions
Action: before_awesome_widget
Function: warning
Priority: 1

Action: before_awesome_widget
Function: puppy
Priority: 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 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' );
	

   	

	

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

	

   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

More Related Content

PDF
WCLV13 JavaScript
PDF
Mocking Demystified
PDF
Forget about Index.php and build you applications around HTTP - PHPers Cracow
PDF
Advanced symfony Techniques
PDF
You Don't Know Query (WordCamp Netherlands 2012)
PDF
PHPSpec - the only Design Tool you need - 4Developers
PDF
PhpSpec 2.0 ilustrated by examples
PDF
Introducing Assetic: Asset Management for PHP 5.3
WCLV13 JavaScript
Mocking Demystified
Forget about Index.php and build you applications around HTTP - PHPers Cracow
Advanced symfony Techniques
You Don't Know Query (WordCamp Netherlands 2012)
PHPSpec - the only Design Tool you need - 4Developers
PhpSpec 2.0 ilustrated by examples
Introducing Assetic: Asset Management for PHP 5.3

What's hot (20)

PPTX
Zero to SOLID
PDF
Decoupling with Design Patterns and Symfony2 DIC
PDF
Design how your objects talk through mocking
PDF
Nubilus Perl
PDF
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
PDF
Decoupling the Ulabox.com monolith. From CRUD to DDD
KEY
Perl Web Client
ODP
Rich domain model with symfony 2.5 and doctrine 2.5
PDF
Min-Maxing Software Costs - Laracon EU 2015
PDF
Как получить чёрный пояс по WordPress?
PDF
Как получить чёрный пояс по WordPress? v2.0
PDF
Virtual Madness @ Etsy
PDF
The IoC Hydra - Dutch PHP Conference 2016
PDF
Plugin jQuery, Design Patterns
PDF
Min-Maxing Software Costs
PDF
Mojolicious
PPTX
Let's write secure Drupal code! DUG Belgium - 08/08/2019
KEY
CodeIgniter 3.0
PPTX
Let's write secure drupal code! - Drupal Camp Pannonia 2019
PDF
jQuery: out with the old, in with the new
Zero to SOLID
Decoupling with Design Patterns and Symfony2 DIC
Design how your objects talk through mocking
Nubilus Perl
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
Decoupling the Ulabox.com monolith. From CRUD to DDD
Perl Web Client
Rich domain model with symfony 2.5 and doctrine 2.5
Min-Maxing Software Costs - Laracon EU 2015
Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress? v2.0
Virtual Madness @ Etsy
The IoC Hydra - Dutch PHP Conference 2016
Plugin jQuery, Design Patterns
Min-Maxing Software Costs
Mojolicious
Let's write secure Drupal code! DUG Belgium - 08/08/2019
CodeIgniter 3.0
Let's write secure drupal code! - Drupal Camp Pannonia 2019
jQuery: out with the old, in with the new

Viewers also liked (20)

PPTX
7 золотых правил внедрения LMS
PDF
IMPM_網路留學Blog
PPTX
Pertemuan 6
PPTX
Pertemuan 11
PPTX
As media course work- evaluation
PPTX
國防報告1 38 39
PPTX
F0306601 Davaakhuu Erdenekhuu
PPTX
Product Gallery 1 2012
PDF
Welcome to Bellingham Washington / Bellingham WA
PDF
Kamiseya Japan / Kamiseya Naval Security Base Japan / Company E Kamiseya Japan
ODP
Who is anyssa
DOCX
Kbsrbunga telur
PPTX
Pertemuan 1tiga
PDF
Le strategie di prevenzione e lo stile di comunicazione. Quando contenuto e f...
PPT
Закрепление пройденного Пропись с. 24
PPT
A Best Practice of Enterprise 2.0: the Photoviva Case Study
PPTX
Collaborationtools aalto 2-10-2012_slideshare
PDF
Продукти для потреб бізнесу та політичної діяльності_Соціологічна агенція "Фама"
PPTX
Шлях ад пачатку стагоддзя: да 115-годдзя з дня нараджэння Міхаіла Ганчарыка
PPT
Resistive Switching
7 золотых правил внедрения LMS
IMPM_網路留學Blog
Pertemuan 6
Pertemuan 11
As media course work- evaluation
國防報告1 38 39
F0306601 Davaakhuu Erdenekhuu
Product Gallery 1 2012
Welcome to Bellingham Washington / Bellingham WA
Kamiseya Japan / Kamiseya Naval Security Base Japan / Company E Kamiseya Japan
Who is anyssa
Kbsrbunga telur
Pertemuan 1tiga
Le strategie di prevenzione e lo stile di comunicazione. Quando contenuto e f...
Закрепление пройденного Пропись с. 24
A Best Practice of Enterprise 2.0: the Photoviva Case Study
Collaborationtools aalto 2-10-2012_slideshare
Продукти для потреб бізнесу та політичної діяльності_Соціологічна агенція "Фама"
Шлях ад пачатку стагоддзя: да 115-годдзя з дня нараджэння Міхаіла Ганчарыка
Resistive Switching

Similar to Hooks WCSD12 (20)

PPTX
Building Your First Widget
PDF
Bending word press to your will
PDF
Introduction to WordPress Hooks 2016
KEY
Actions filters
PDF
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
KEY
WordPress Bootcamp Part 2 - Extending WordPress
PPTX
2016 WordCamp Pittsburgh - Let's Write a Plugin
PPTX
Creating Customizable Widgets for Unpredictable Needs
ODP
5 W's of Hookin'
PPTX
Let’s write a plugin
PDF
Jumping Into WordPress Plugin Programming
PDF
Plugin development demystified 2017
PPTX
Wordpress hooks
PPT
WordPress development paradigms, idiosyncrasies and other big words
PDF
Write your first WordPress plugin
PDF
<Head> Presentation: Plugging Into Wordpress
PDF
Best Practices in Plugin Development (WordCamp Seattle)
PDF
Living With Legacy Code
PDF
WordPress Theme Workshop: Widgets
ODP
Beginning WordPress Plugin Development
Building Your First Widget
Bending word press to your will
Introduction to WordPress Hooks 2016
Actions filters
Stop Hacking WordPress, Start Working with it - Charly Leetham - WordCamp Syd...
WordPress Bootcamp Part 2 - Extending WordPress
2016 WordCamp Pittsburgh - Let's Write a Plugin
Creating Customizable Widgets for Unpredictable Needs
5 W's of Hookin'
Let’s write a plugin
Jumping Into WordPress Plugin Programming
Plugin development demystified 2017
Wordpress hooks
WordPress development paradigms, idiosyncrasies and other big words
Write your first WordPress plugin
<Head> Presentation: Plugging Into Wordpress
Best Practices in Plugin Development (WordCamp Seattle)
Living With Legacy Code
WordPress Theme Workshop: Widgets
Beginning WordPress Plugin Development

Recently uploaded (20)

PDF
Chapter 1: computer maintenance and troubleshooting
PPTX
Information-Technology-in-Human-Society (2).pptx
PDF
Addressing the challenges of harmonizing law and artificial intelligence tech...
PDF
Streamline Vulnerability Management From Minimal Images to SBOMs
PDF
GDG Cloud Southlake #45: Patrick Debois: The Impact of GenAI on Development a...
PDF
substrate PowerPoint Presentation basic one
PDF
Internet of Things (IoT) – Definition, Types, and Uses
PPTX
Blending method and technology for hydrogen.pptx
PDF
The AI Revolution in Customer Service - 2025
PDF
ELLIE29.pdfWETWETAWTAWETAETAETERTRTERTER
PPTX
maintenance powerrpoint for adaprive and preventive
PPTX
From XAI to XEE through Influence and Provenance.Controlling model fairness o...
PPT
Overviiew on Intellectual property right
PDF
NewMind AI Journal Monthly Chronicles - August 2025
PPTX
Build automations faster and more reliably with UiPath ScreenPlay
PPTX
Rise of the Digital Control Grid Zeee Media and Hope and Tivon FTWProject.com
PDF
Gestión Unificada de los Riegos Externos
PDF
Connector Corner: Transform Unstructured Documents with Agentic Automation
PDF
Advancements in abstractive text summarization: a deep learning approach
PDF
Introduction to c language from lecture slides
Chapter 1: computer maintenance and troubleshooting
Information-Technology-in-Human-Society (2).pptx
Addressing the challenges of harmonizing law and artificial intelligence tech...
Streamline Vulnerability Management From Minimal Images to SBOMs
GDG Cloud Southlake #45: Patrick Debois: The Impact of GenAI on Development a...
substrate PowerPoint Presentation basic one
Internet of Things (IoT) – Definition, Types, and Uses
Blending method and technology for hydrogen.pptx
The AI Revolution in Customer Service - 2025
ELLIE29.pdfWETWETAWTAWETAETAETERTRTERTER
maintenance powerrpoint for adaprive and preventive
From XAI to XEE through Influence and Provenance.Controlling model fairness o...
Overviiew on Intellectual property right
NewMind AI Journal Monthly Chronicles - August 2025
Build automations faster and more reliably with UiPath ScreenPlay
Rise of the Digital Control Grid Zeee Media and Hope and Tivon FTWProject.com
Gestión Unificada de los Riegos Externos
Connector Corner: Transform Unstructured Documents with Agentic Automation
Advancements in abstractive text summarization: a deep learning approach
Introduction to c language from lecture slides

Hooks WCSD12

  • 1. WordPress Hooks Actions and Filters WordCamp San Diego 2012
  • 2. Jeffrey Zinn ocean 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 • [email protected]
  • 3. 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.
  • 4. Wait...What are Hooks? •Actions - Do Stuff • Filters - Change Stuff
  • 5. Why Hooks? • Crack into code without editing core files • Let others alter your work
  • 6. Hypothetical Let’s say we were desperate for a widget to place in our sidebar that displays the word “awesome.”
  • 8. Basic Awesome Widget sidebar activate
  • 9. Basic Awesome Widget sidebar activate test
  • 10. Hypothetical Upon further consideration you realize this widget is sorely lacking in puppy pictures. Now what? Let’s look in the code...
  • 11. Basic Widget Code public 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; }
  • 12. Basic Widget Code public 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; }
  • 13. Deluxe Awesome Widget t wi dge sam e
  • 14. 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' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget; }
  • 15. 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' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget; }
  • 16. 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; }
  • 17. 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; }
  • 18. 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; }
  • 19. 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; }
  • 20. 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
  • 21. 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...
  • 22. 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...
  • 23. 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' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget; }
  • 24. 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' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget; }
  • 25. 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; }
  • 26. 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....
  • 27. 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....
  • 28. 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....
  • 30. Multiple Calls to Action add_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....
  • 31. Multiple Calls to Action add_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....
  • 32. Multiple Calls to Action add_action( ‘before_awesome_widget’, ‘puppy’ ); function puppy() { $src = 'http://placedog.com/g/188/150'; Diff. F Same echo '<img src="' . $src . '" />'; unctio Action } ns add_action( ‘before_awesome_widget’, ‘warning’ ); function warning() { echo '<p>Warning: puppies!</p>'; } ...in your functions.php script....
  • 34. Multiple Calls to Action Action: before_awesome_widget Function: puppy
  • 35. Multiple Calls to Action Action: before_awesome_widget Function: puppy Action: before_awesome_widget Function: warning
  • 36. Ordering Actions add_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....
  • 39. Ordering Actions Action: before_awesome_widget Function: warning Priority: 1 Action: before_awesome_widget Function: puppy Priority: 2
  • 41. 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
  • 42. 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' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget; }
  • 43. 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; }
  • 44. 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
  • 45. 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
  • 46. 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
  • 47. 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....
  • 48. 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....
  • 49. 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....
  • 50. 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....
  • 51. 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....
  • 52. 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....
  • 53. 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....
  • 54. 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....
  • 55. 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....
  • 56. 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....
  • 57. 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....
  • 58. 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....
  • 59. 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....
  • 60. 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....
  • 61. 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....
  • 63. 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 ); echo apply_filters( 'filter_ridiculous_text', $awesome, $wrap, $text ); do_action( 'after_ridiculous_widget' ); echo $after_widget; }
  • 64. 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; }
  • 65. 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 );
  • 66. 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>
  • 67. 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>
  • 68. 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....
  • 69. 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....
  • 70. 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....
  • 71. 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....
  • 72. 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....
  • 73. 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....
  • 74. 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....
  • 75. 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....
  • 80. 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
  • 81. Where Hooks? • Plugin/Theme Wiki • Documentation • Actual Code (search for do_action and apply_filters) • Google search! • http://codex.wordpress.org/Plugin_API
  • 82. Yes, Hooks! •Actions - Do Stuff • Filters - Change Stuff
  • 83. 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