Your browser doesn't support the features required by impress.js, so you are presented with a simplified version of this presentation.

For the best experience please use the latest Chrome, Safari or Firefox browser.

Use a spacebar or arrow keys to navigate

WV305 WordPress Plugin Development

Session 2: Hooks

Erick Hitter

cdia@ethitter.com

What is a hook?

In WordPress parlance, a hook is something that lets your plugin (or theme) interact with WordPress in a different way than functions and classes allow.

 

These interactions might let you modify how WordPress behaves, or change some value somewhere within WordPress.

 

Hundreds of these hooks are present throughout WordPress.

How do they work?

 

Convoluted explanation, no?

 

Think of these as similar to Javascript events.

Examples

Types of Hooks

Actions

Comprised of two functions:

do_action( $tag, $arg = '' )

add_action( $tag, $function_to_add, $priority = 10, $accepted_args = 1 )

do_action()

 

Example:

do_action( 'after_setup_theme' );

add_action()

 

Example:

/**
 * Tell WordPress to run twentyeleven_setup() when the 'after_setup_theme' hook is run.
 */
add_action( 'after_setup_theme', 'twentyeleven_setup' );

Filters

Comprised of two functions:

apply_filters( $tag, $value )

add_filter( $tag, $function_to_add, $priority = 10, $accepted_args = 1 )

 

Look familiar?

apply_filters()

 

Example:

$excerpt_length = apply_filters('excerpt_length', 55);

add_filter()

 

Example:

add_filter( 'excerpt_length', 'twentyeleven_excerpt_length' );

Actions vs Filters

Actions

Filters

Do something Change something
Provide data for use in the function, but don't take it back. Provide a value to be modified and must get something back.
Can echo things within. Never echo things within.

Guess what? That last slide lied.

Before I explain why, any questions?

Resources