5 Ws of Hookin

35
5 W's of Hookin' Nowell VanHoesen @NowellVanHoesen about.me/NowellVanHoesen

Transcript of 5 Ws of Hookin

5 W's of Hookin'

Nowell VanHoesen@NowellVanHoesen

about.me/NowellVanHoesen

Hookin' Overview

What are Hooks

Who hooks

Why hook

Where are they

When to hook

Examples

@NowellVanHoesen5 W's of Hookin'

What are hooks?

A specific place/time in WordPress code execution to add functionality or change data.

@NowellVanHoesen5 W's of Hookin'

What are hooks?

A specific place/time in WordPress code execution to add functionality or change data.

Types:

– Action: add functionality at a specific point when an event happens or is about to happen.

@NowellVanHoesen5 W's of Hookin'

What are hooks?

A specific place/time in WordPress code execution to add functionality or change data.

Types:

– Action: add functionality at a specific point when an event happens or is about to happen.

– Filter: modify data before some event( save, display )

@NowellVanHoesen5 W's of Hookin'

Who hooks?

@NowellVanHoesen5 W's of Hookin'

Who hooks?

Any one who wants to add custom code to change how WordPress behaves or modify what is output to the browser.

@NowellVanHoesen5 W's of Hookin'

Who hooks?

Any one who wants to add custom code to change how WordPress behaves or modify what is output to the browser.

Anyone who wants to allow others to build on to their plugin or theme.

@NowellVanHoesen5 W's of Hookin'

Why

@NowellVanHoesen5 W's of Hookin'

Why

use hooks

@NowellVanHoesen5 W's of Hookin'

Why

use hooks● Tweak

functionality/output to better fit your needs

@NowellVanHoesen5 W's of Hookin'

Why

use hooks● Tweak

functionality/output to better fit your needs

● Maintain your customizations when updating

@NowellVanHoesen5 W's of Hookin'

Why

use hooks● Tweak

functionality/output to better fit your needs

● Maintain your customizations when updating

create hooks

@NowellVanHoesen5 W's of Hookin'

Why

use hooks● Tweak

functionality/output to better fit your needs

● Maintain your customizations when updating

create hooks● Allow others to add

functionality

@NowellVanHoesen5 W's of Hookin'

Why

use hooks● Tweak

functionality/output to better fit your needs

● Maintain your customizations when updating

create hooks● Allow others to add

functionality

● Allow others to filter output from your plugin/theme

@NowellVanHoesen5 W's of Hookin'

Where...

can I find them

@NowellVanHoesen5 W's of Hookin'

Where...

can I find them● WordPress core

@NowellVanHoesen5 W's of Hookin'

Where...

can I find them● WordPress core

● Themes

@NowellVanHoesen5 W's of Hookin'

Where...

can I find them● WordPress core

● Themes

● Plugins

@NowellVanHoesen5 W's of Hookin'

Where...

can I find them● WordPress core

● Themes

● Plugins

can I create them

@NowellVanHoesen5 W's of Hookin'

Where...

can I find them● WordPress core

● Themes

● Plugins

can I create them● Themes

@NowellVanHoesen5 W's of Hookin'

Where...

can I find them● WordPress core

● Themes

● Plugins

can I create them● Themes

● Plugins

@NowellVanHoesen5 W's of Hookin'

Where...

can I find them● WordPress core

● Themes

● Plugins

can I create them● Themes

● Plugins

● WordPress core

@NowellVanHoesen5 W's of Hookin'

Where...

can I find them● WordPress core

● Themes

● Plugins

can I create them● Themes

● Plugins

● WordPress core

@NowellVanHoesen5 W's of Hookin'

How?

Actions Filters

@NowellVanHoesen5 W's of Hookin'

How?

Actions● add_action( t, f, p, a )

Filters● add_filter( t, f, p, a )

@NowellVanHoesen5 W's of Hookin'

t = tag f = function p = priority a = # args

How?

Actions● add_action( t, f, p, a )

● remove_action( t, f, p, a )

Filters● add_filter( t, f, p, a )

● remove_filter( t, f, p, a )

@NowellVanHoesen5 W's of Hookin'

t = tag f = function p = priority a = # args

How?

Actions● add_action( t, f, p, a )

● remove_action( t, f, p, a )

● do_action( t, f, p, a )

Filters● add_filter( t, f, p, a )

● remove_filter( t, f, p, a )

● apply_filters( t, f, p, a )

@NowellVanHoesen5 W's of Hookin'

t = tag f = function p = priority a = # args

How?

Actions● add_action( t, f, p, a )

● remove_action( t, f, p, a )

● do_action( t, f, p, a )

Filters● add_filter( t, f, p, a )

● remove_filter( t, f, p, a )

● apply_filters( t, f, p, a )

@NowellVanHoesen5 W's of Hookin'

http://codex.wordpress.org/Plugin_APIt = tag f = function p = priority a = # args

Action hook example 1// wp_enqueue_scripts actionadd_action( 'wp_enqueue_scripts', 'nv_added_styles' );

function nv_added_styles() { if ( !is_admin() ) { wp_register_style( 'nv-ie-fix', get_bloginfo( 'stylesheet_directory' ) . '/ie.css', false ); $GLOBALS['wp_styles']->add_data( 'nv-ie-fix', 'conditional', 'lte IE 8' ); wp_enqueue_style( 'nv-ie-fix' ); }}

@NowellVanHoesen5 W's of Hookin'

Action hook example 2add_action( 'pre_get_posts', 'my_get_posts' );function my_get_posts( $query ) { global $wp_the_query; if( $wp_the_query === $query && $query->is_home() ) { add_filter( 'posts_where' , 'posts_where' ); $category_id = get_cat_ID( 'Scribblings' ); $query->set( 'cat', $category_id ); } else if ( is_archive() && is_date() && !is_category()) { if ( !isset( $query->query_vars['post_type'] ) ) { $query->set( 'post_type', array( 'post', 'myoldhouse', 'snapshots' ) ); $query->set( 'posts_per_page', -1 ); } }}function posts_where( $where ) { $where = " AND ( ( wp_term_relationships.term_taxonomy_id IN (1) AND wp_posts.post_type = 'post' ) OR ( wp_posts.post_type IN ('myoldhouse', 'snapshots') ) ) AND (wp_posts.post_status = 'publish' OR wp_posts.post_status = 'private')"; return $where;}

@NowellVanHoesen5 W's of Hookin'

Filter hook examplesadd_filter( 'the_title', 'nv_the_title', 10, 2 );

function nv_the_title( $title, $id ) { if ( !is_admin() ) { $title = 'WCCbus - ' . $title; } return $title;}

// what core runsreturn apply_filters( 'the_title', $title, $id );

add_filter( 'enter_title_here', 'change_default_post_title', 10, 2 );

function change_default_post_title( $text, $post ) { if ( 'post' == get_post_type( $post ) ) { return $text . “ - Required”; }}

@NowellVanHoesen5 W's of Hookin'

Time to go live...

@NowellVanHoesen5 W's of Hookin'

Time to go live...

@NowellVanHoesen5 W's of Hookin'

Resources

@NowellVanHoesen5 W's of Hookin'

Plugin API on the codex

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

Drew Jaynes – Filter of the Day: Three filters a day for 365 days

- http://fotd.werdswords.com/