SlideShare a Scribd company logo
1 of 83
Download to read offline
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....
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

What's hot

Decoupling with Design Patterns and Symfony2 DIC
Decoupling with Design Patterns and Symfony2 DICDecoupling with Design Patterns and Symfony2 DIC
Decoupling with Design Patterns and Symfony2 DICKonstantin Kudryashov
 
Design how your objects talk through mocking
Design how your objects talk through mockingDesign how your objects talk through mocking
Design how your objects talk through mockingKonstantin Kudryashov
 
New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)New Symfony Tips & Tricks (SymfonyCon Paris 2015)
New Symfony Tips & Tricks (SymfonyCon Paris 2015)Javier Eguiluz
 
Decoupling the Ulabox.com monolith. From CRUD to DDD
Decoupling the Ulabox.com monolith. From CRUD to DDDDecoupling the Ulabox.com monolith. From CRUD to DDD
Decoupling the Ulabox.com monolith. From CRUD to DDDAleix Vergés
 
Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Rich domain model with symfony 2.5 and doctrine 2.5
Rich domain model with symfony 2.5 and doctrine 2.5Leonardo Proietti
 
Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015Min-Maxing Software Costs - Laracon EU 2015
Min-Maxing Software Costs - Laracon EU 2015Konstantin Kudryashov
 
Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?Как получить чёрный пояс по WordPress?
Как получить чёрный пояс по WordPress?Yevhen Kotelnytskyi
 
Как получить чёрный пояс по WordPress? v2.0
Как получить чёрный пояс по WordPress? v2.0Как получить чёрный пояс по WordPress? v2.0
Как получить чёрный пояс по WordPress? v2.0Yevhen Kotelnytskyi
 
Virtual Madness @ Etsy
Virtual Madness @ EtsyVirtual Madness @ Etsy
Virtual Madness @ EtsyNishan Subedi
 
The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016The IoC Hydra - Dutch PHP Conference 2016
The IoC Hydra - Dutch PHP Conference 2016Kacper Gunia
 
Plugin jQuery, Design Patterns
Plugin jQuery, Design PatternsPlugin jQuery, Design Patterns
Plugin jQuery, Design PatternsRobert Casanova
 
Let's write secure Drupal code! DUG Belgium - 08/08/2019
Let's write secure Drupal code! DUG Belgium - 08/08/2019Let's write secure Drupal code! DUG Belgium - 08/08/2019
Let's write secure Drupal code! DUG Belgium - 08/08/2019Balázs Tatár
 
Let's write secure drupal code! - Drupal Camp Pannonia 2019
Let's write secure drupal code! - Drupal Camp Pannonia 2019Let's write secure drupal code! - Drupal Camp Pannonia 2019
Let's write secure drupal code! - Drupal Camp Pannonia 2019Balázs Tatár
 
jQuery: out with the old, in with the new
jQuery: out with the old, in with the newjQuery: out with the old, in with the new
jQuery: out with the old, in with the newRemy Sharp
 

What's hot (20)

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

Viewers also liked

7 золотых правил внедрения LMS
7 золотых правил внедрения LMS7 золотых правил внедрения LMS
7 золотых правил внедрения LMSAlexander Andreev
 
As media course work- evaluation
As media course work- evaluationAs media course work- evaluation
As media course work- evaluationBillysmedia
 
國防報告1 38 39
國防報告1 38 39國防報告1 38 39
國防報告1 38 39TFGYi12
 
F0306601 Davaakhuu Erdenekhuu
F0306601 Davaakhuu Erdenekhuu F0306601 Davaakhuu Erdenekhuu
F0306601 Davaakhuu Erdenekhuu Sasha Da
 
Welcome to Bellingham Washington / Bellingham WA
Welcome to Bellingham Washington / Bellingham WAWelcome to Bellingham Washington / Bellingham WA
Welcome to Bellingham Washington / Bellingham WARich Johnson
 
Kamiseya Japan / Kamiseya Naval Security Base Japan / Company E Kamiseya Japan
Kamiseya Japan / Kamiseya Naval Security Base Japan / Company E Kamiseya JapanKamiseya Japan / Kamiseya Naval Security Base Japan / Company E Kamiseya Japan
Kamiseya Japan / Kamiseya Naval Security Base Japan / Company E Kamiseya JapanRich Johnson
 
Le strategie di prevenzione e lo stile di comunicazione. Quando contenuto e f...
Le strategie di prevenzione e lo stile di comunicazione. Quando contenuto e f...Le strategie di prevenzione e lo stile di comunicazione. Quando contenuto e f...
Le strategie di prevenzione e lo stile di comunicazione. Quando contenuto e f...Giuseppe Fattori
 
Закрепление пройденного Пропись с. 24
Закрепление пройденного Пропись с. 24Закрепление пройденного Пропись с. 24
Закрепление пройденного Пропись с. 24МКОУ СОШ № 1 г. Сим
 
A Best Practice of Enterprise 2.0: the Photoviva Case Study
A Best Practice of Enterprise 2.0: the Photoviva Case StudyA Best Practice of Enterprise 2.0: the Photoviva Case Study
A Best Practice of Enterprise 2.0: the Photoviva Case StudyWalter Del Prete
 
Collaborationtools aalto 2-10-2012_slideshare
Collaborationtools aalto 2-10-2012_slideshareCollaborationtools aalto 2-10-2012_slideshare
Collaborationtools aalto 2-10-2012_slideshareTero Peltola
 
Продукти для потреб бізнесу та політичної діяльності_Соціологічна агенція "Фама"
Продукти для потреб бізнесу та політичної діяльності_Соціологічна агенція "Фама"Продукти для потреб бізнесу та політичної діяльності_Соціологічна агенція "Фама"
Продукти для потреб бізнесу та політичної діяльності_Соціологічна агенція "Фама"Yarynka Voznyak
 

Viewers also liked (20)

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

Similar to Hooks WCSD12

Building Robust jQuery Plugins
Building Robust jQuery PluginsBuilding Robust jQuery Plugins
Building Robust jQuery PluginsJörn Zaefferer
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretssmueller_sandsmedia
 
Using Actions and Filters in WordPress to Make a Plugin Your Own
Using Actions and Filters in WordPress to Make a Plugin Your OwnUsing Actions and Filters in WordPress to Make a Plugin Your Own
Using Actions and Filters in WordPress to Make a Plugin Your OwnBrian Hogg
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015Fernando Daciuk
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptjQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptGuy Royse
 
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needDutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needKacper Gunia
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologyDaniel Knell
 
Avinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPressAvinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPresswpnepal
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11Michelangelo van Dam
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxMichelangelo van Dam
 
jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Knowgirish82
 
jQuery - Introdução
jQuery - IntroduçãojQuery - Introdução
jQuery - IntroduçãoGustavo Dutra
 
JQuery do dia-a-dia Gustavo Dutra
JQuery do dia-a-dia Gustavo DutraJQuery do dia-a-dia Gustavo Dutra
JQuery do dia-a-dia Gustavo DutraTchelinux
 

Similar to Hooks WCSD12 (20)

Building Robust jQuery Plugins
Building Robust jQuery PluginsBuilding Robust jQuery Plugins
Building Robust jQuery Plugins
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
international PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secretsinternational PHP2011_Bastian Feder_jQuery's Secrets
international PHP2011_Bastian Feder_jQuery's Secrets
 
Using Actions and Filters in WordPress to Make a Plugin Your Own
Using Actions and Filters in WordPress to Make a Plugin Your OwnUsing Actions and Filters in WordPress to Make a Plugin Your Own
Using Actions and Filters in WordPress to Make a Plugin Your Own
 
Separation of concerns - DPC12
Separation of concerns - DPC12Separation of concerns - DPC12
Separation of concerns - DPC12
 
WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015WordPress Realtime - WordCamp São Paulo 2015
WordPress Realtime - WordCamp São Paulo 2015
 
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScriptjQuery & 10,000 Global Functions: Working with Legacy JavaScript
jQuery & 10,000 Global Functions: Working with Legacy JavaScript
 
Unittests für Dummies
Unittests für DummiesUnittests für Dummies
Unittests für Dummies
 
jQuery secrets
jQuery secretsjQuery secrets
jQuery secrets
 
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you needDutch PHP Conference - PHPSpec 2 - The only Design Tool you need
Dutch PHP Conference - PHPSpec 2 - The only Design Tool you need
 
Symfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technologySymfony2 Building on Alpha / Beta technology
Symfony2 Building on Alpha / Beta technology
 
Avinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPressAvinash Kundaliya: Javascript and WordPress
Avinash Kundaliya: Javascript and WordPress
 
Unit testing with zend framework tek11
Unit testing with zend framework tek11Unit testing with zend framework tek11
Unit testing with zend framework tek11
 
Unit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBeneluxUnit testing with zend framework PHPBenelux
Unit testing with zend framework PHPBenelux
 
jQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't KnowjQuery - 10 Time-Savers You (Maybe) Don't Know
jQuery - 10 Time-Savers You (Maybe) Don't Know
 
Rails is not just Ruby
Rails is not just RubyRails is not just Ruby
Rails is not just Ruby
 
Clean Javascript
Clean JavascriptClean Javascript
Clean Javascript
 
jQuery - Introdução
jQuery - IntroduçãojQuery - Introdução
jQuery - Introdução
 
JQuery do dia-a-dia Gustavo Dutra
JQuery do dia-a-dia Gustavo DutraJQuery do dia-a-dia Gustavo Dutra
JQuery do dia-a-dia Gustavo Dutra
 
J queryui
J queryuiJ queryui
J queryui
 

Recently uploaded

A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxLoriGlavin3
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditSkynet Technologies
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Hiroshi SHIBATA
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Farhan Tariq
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Mark Goldstein
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsNathaniel Shimoni
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Scott Andery
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 

Recently uploaded (20)

A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptxDigital Identity is Under Attack: FIDO Paris Seminar.pptx
Digital Identity is Under Attack: FIDO Paris Seminar.pptx
 
Manual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance AuditManual 508 Accessibility Compliance Audit
Manual 508 Accessibility Compliance Audit
 
Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024Long journey of Ruby standard library at RubyConf AU 2024
Long journey of Ruby standard library at RubyConf AU 2024
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...Genislab builds better products and faster go-to-market with Lean project man...
Genislab builds better products and faster go-to-market with Lean project man...
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
Arizona Broadband Policy Past, Present, and Future Presentation 3/25/24
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Time Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directionsTime Series Foundation Models - current state and future directions
Time Series Foundation Models - current state and future directions
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
Enhancing User Experience - Exploring the Latest Features of Tallyman Axis Lo...
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 

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 • jeff@jzinn.us
  • 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....
  • 29.
  • 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