83
WordPress Hooks Actions and Filters WordCamp San Diego 2012

Hooks WCSD12

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: Hooks WCSD12

WordPress HooksActions and Filters

WordCamp San Diego 2012

Page 2: Hooks WCSD12

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]

Page 3: Hooks WCSD12

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.

Page 4: Hooks WCSD12

Wait...What are Hooks?

•Actions - Do Stuff

•Filters - Change Stuff

Page 5: Hooks WCSD12

Why Hooks?

• Crack into code without editing core files

• Let others alter your work

Page 6: Hooks WCSD12

HypotheticalLet’s say we were desperate for a widget to place in our sidebar that displays the word “awesome.”

Page 7: Hooks WCSD12

Basic Awesome Widget

activate

Page 8: Hooks WCSD12

Basic Awesome Widget

activate

sidebar

Page 9: Hooks WCSD12

Basic Awesome Widget

activate

sidebar

test

Page 10: Hooks WCSD12

HypotheticalUpon further consideration you realize this widget is sorely lacking in puppy pictures. Now what? Let’s look in the code...

Page 11: Hooks WCSD12

Basic Widget Codepublic function widget( $args, $instance ) {

extract( $args ); $title = $instance['title'];

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; echo '<h2>awesome</h2>'; echo $after_widget;}

Page 12: Hooks WCSD12

Basic Widget Codepublic function widget( $args, $instance ) {

extract( $args ); $title = $instance['title'];

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; echo '<h2>awesome</h2>'; echo $after_widget;}

Page 13: Hooks WCSD12

Deluxe Awesome Widget

widgetsame

Page 14: Hooks WCSD12

Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_awesome_widget' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget;}

Page 15: Hooks WCSD12

Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_awesome_widget' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget;}

Page 16: Hooks WCSD12

Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_awesome_widget' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget;}

filter!

Page 17: Hooks WCSD12

Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_awesome_widget' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget;}

action!

filter!

Page 18: Hooks WCSD12

Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_awesome_widget' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget;}

action!

filter!

filter!

Page 19: Hooks WCSD12

Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_awesome_widget' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget;}

action!

action!

filter!

filter!

Page 20: Hooks WCSD12

Actions

• 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

(do stuff)

Page 21: Hooks WCSD12

Actions: do_action()

• $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: ''

do_action( $tag, $arg, $extra_arg );

...their code...

Page 22: Hooks WCSD12

Actions: add_action()

• $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

add_action( $tag, $function_to_add, $priority, $accepted_args );

...our code...

Page 23: Hooks WCSD12

Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_awesome_widget' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget;}

Page 24: Hooks WCSD12

Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_awesome_widget' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget;}

Page 25: Hooks WCSD12

Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_awesome_widget' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget;}

action!

Page 26: Hooks WCSD12

add_action( ‘before_awesome_widget’, ‘puppy’ );

function puppy() {$src = 'http://placedog.com/g/188/150';echo '<img src="' . $src . '" />';

}

...in your functions.php script....

which action? which function of ours?

do_action( 'before_awesome_widget' );

Page 27: Hooks WCSD12

add_action( ‘before_awesome_widget’, ‘puppy’ );

function puppy() {$src = 'http://placedog.com/g/188/150';echo '<img src="' . $src . '" />';

}

...in your functions.php script....

which action? which function of ours?

do_action( 'before_awesome_widget' );

Page 28: Hooks WCSD12

add_action( ‘before_awesome_widget’, ‘puppy’ );

function puppy() {$src = 'http://placedog.com/g/188/150';echo '<img src="' . $src . '" />';

}

...in your functions.php script....

which action? which function of ours?

do_action( 'before_awesome_widget' );

Use a unique function name

Page 29: Hooks WCSD12
Page 30: Hooks WCSD12

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

Page 33: Hooks WCSD12

Multiple Calls to Action

Page 34: Hooks WCSD12

Multiple Calls to Action

Action: before_awesome_widgetFunction: puppy

Page 35: Hooks WCSD12

Multiple Calls to Action

Action: before_awesome_widgetFunction: puppy

Action: before_awesome_widgetFunction: warning

Page 36: Hooks WCSD12

$priority (default 10)

...in your functions.php script....

Ordering Actionsadd_action( ‘before_awesome_widget’, ‘puppy’, 2 );function puppy() {

$src = 'http://placedog.com/g/188/150';echo '<img src="' . $src . '" />';

}

add_action( ‘before_awesome_widget’, ‘warning’, 1 );function warning() {

echo '<p>Warning: puppies!</p>';}

Page 37: Hooks WCSD12

Ordering Actions

Page 38: Hooks WCSD12

Ordering ActionsAction: before_awesome_widgetFunction: warningPriority: 1

Page 39: Hooks WCSD12

Ordering ActionsAction: before_awesome_widgetFunction: warningPriority: 1

Action: before_awesome_widgetFunction: puppyPriority: 2

Page 40: Hooks WCSD12

remove_action( $tag, $function_to_remove, $priority, $accepted_args );

remove_action( 'before_awesome_widget', 'warning', 1 );

Actions: remove_action()

Page 41: Hooks WCSD12

remove_action( $tag, $function_to_remove, $priority, $accepted_args );

remove_action( 'before_awesome_widget', 'warning', 1 );

Be as specific as the original action

Actions: remove_action()

Page 42: Hooks WCSD12

Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_awesome_widget' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget;}

Page 43: Hooks WCSD12

Deluxe Widget Codepublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_awesome_widget' ); echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' ); do_action( 'after_awesome_widget' ); echo $after_widget;}

filter!

Page 44: Hooks WCSD12

Filters

• 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

(change text)

Page 45: Hooks WCSD12

Filters: apply_filters()

• $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

apply_filters( $tag, $value, $var ... );

Page 46: Hooks WCSD12

Filters: add_filter()

• $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

add_filter( $tag, $function_to_add, $priority, $accepted_args );

Page 47: Hooks WCSD12

add_filter( ‘filter_awesome_text’, ‘filter_awesome’ );

function filter_awesome( $text ) {$text .= ‘<h4>puppies</h4>’;

return $text;}

...in your functions.php script....

which filter? which function of ours?

echo apply_filters( 'filter_awesome_text', '<h2>awesome</h2>' );

Page 48: Hooks WCSD12

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

Use a unique function name

Page 49: Hooks WCSD12

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

Page 50: Hooks WCSD12

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

always return!

Page 51: Hooks WCSD12

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

always return!

Page 52: Hooks WCSD12

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

Page 53: Hooks WCSD12

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

Page 54: Hooks WCSD12

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

Page 55: Hooks WCSD12

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

Page 56: Hooks WCSD12

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

Page 57: Hooks WCSD12

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

Page 58: Hooks WCSD12

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

Page 59: Hooks WCSD12

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

Page 60: Hooks WCSD12

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

Page 61: Hooks WCSD12

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

Page 62: Hooks WCSD12

remove_filter()

remove_filter( $tag, $function_to_remove, $priority, $accepted_args );

remove_filter( 'the_content', 'wpautop' );

Page 63: Hooks WCSD12

Ridiculous Filterpublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_ridiculous_widget' ); $wrap = ( is_home() ) ? 'h2' : 'h3'; $text = 'awesome'; $pattern = '<%s class="awesome">%s</%s>'; $awesome = sprintf( $pattern, $wrap, $text, $wrap ); echo apply_filters( 'filter_ridiculous_text', $awesome, $wrap, $text ); do_action( 'after_ridiculous_widget' ); echo $after_widget;}

Page 64: Hooks WCSD12

Ridiculous Filterpublic function widget( $args, $instance ) { extract( $args ); $title = apply_filters( 'widget_title', $instance['title'] );

echo $before_widget; if ( ! empty( $title ) ) echo $before_title . $title . $after_title; do_action( 'before_ridiculous_widget' ); $wrap = ( is_home() ) ? 'h2' : 'h3'; $text = 'awesome'; $pattern = '<%s class="awesome">%s</%s>'; $awesome = sprintf( $pattern, $wrap, $text, $wrap ); echo apply_filters( 'filter_ridiculous_text', $awesome, $wrap, $text ); do_action( 'after_ridiculous_widget' ); echo $after_widget;}

filter!

Page 65: Hooks WCSD12

$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

Page 66: Hooks WCSD12

$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

is_home()

<h2>awesome</h2>

!is_home()

<h3>awesome</h3>

Page 67: Hooks WCSD12

$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

is_home()

<h2>awesome</h2>

!is_home()

<h3>awesome</h3>

??????

Page 68: Hooks WCSD12

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()...

Page 69: Hooks WCSD12

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()...

Page 70: Hooks WCSD12

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()...

Page 71: Hooks WCSD12

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

<h2>awesome</h2>...assume is_home()...

Page 72: Hooks WCSD12

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

<h2>awesome</h2> h2...assume is_home()...

Page 73: Hooks WCSD12

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

<h2>awesome</h2> h2 awesome...assume is_home()...

Page 74: Hooks WCSD12

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()...

Page 75: Hooks WCSD12

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()...

Page 76: Hooks WCSD12

Examples

Page 77: Hooks WCSD12

Genesis Framework

Page 78: Hooks WCSD12

Genesis Actions

Page 79: Hooks WCSD12

Genesis Filters

Page 80: Hooks WCSD12

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

Page 81: Hooks WCSD12

Where Hooks?

• Plugin/Theme Wiki

• Documentation

• Actual Code (search for do_action and apply_filters)

• Google search!

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

Page 82: Hooks WCSD12

Yes, Hooks!

•Actions - Do Stuff

•Filters - Change Stuff

Page 83: Hooks WCSD12

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