There's A HOOK for that

James Bonham
Team lead @
James Bonham
Organiser @



– CODEX
A hook is an event... invoked by do_action() or apply_filters()

– less geeky person
A hook GIVES YOU THE MEANS TO Do something ELSE OR MODIFY existing stuff

action HOOK
DO SOMETHING WHEN A specific event take place in WordPress, such as publishing a post
Do Something
action HOOK
add_action() hooks a function onto the specified hook.
do_action() executes all THE hooked functions for a given hook, at the right moment.
Do Something
Action HOOK: Concept
When the sun shines, COOL DOWN THE OFFICE and close some blinds
Action HOOK: Concept
sunshine = Event where we need a hook cool_down_office = Action to add to hook close_blinds = Action to add to hook
Action HOOK: Concept
Make it sunny
COOL IT DOWN
MAKE IT HAPPEN
function sunshine() {
remove_clouds();
do_action( 'sunny' );
}
function cool_down_office() {
$temp = get_aircon_temp();
if ( $temp > 21 ) {
set_aircon_temp( 18 );
}
}
add_action( 'sunny', 'cool_down_office', 1 );
Action HOOK: Concept
Close blinds
MAKE IT HAPPEN
function close_blinds() {
$blinds = get_blinds('roof');
foreach ( $blinds as $blind ) {
close_blind( $blind );
}
}
add_action( 'sunny', 'close_blinds', 2 );
PAGE EXCERPTS
DO SOMETHING
/**
* Add excerpts to pages.
*/
function xyz_support_page_excerpts() {
add_post_type_support( 'page', 'excerpt' );
}
add_action( 'init', 'xyz_support_page_excerpts' );
Search ALL
DO SOMETHING
/**
* Override post types search should include.
*
* $query object The query to be run.
*/
function xyz_search_more( $query ) {
if ( ! is_admin() && $query->is_main_query() ) {
if ( $query->is_search ) {
$query->set( 'post_type', ['post', 'page', 'movie'] );
}
}
}
add_action( 'pre_get_posts', 'xyz_search_more' );
NOTIFY
DO SOMETHING
/**
* Email me when someone publishes.
*/
function xyz_email_me( $id, $post ) {
wp_mail( 'jb@peytz.dk', "Blog updated", ...
...'Hey, new Content! ' . get_permalink( $id ) );
}
add_action( 'publish_post', 'xyz_email_me' );
option revisions
DO SOMETHING
/**
* Updates the log
*
* @param string $option Name of the option that has been updated
* @param string $old_value The old value before the user changed it
* @param string $new_value The new value that the user changed it to
* @since 1.0.0
*/
function log_option_change( $option, $old_value, $value ) {
if( $old_value == $value || $option == 'option_logger' ) {
return;
}
if( substr( $option, 0, 1 ) == '_' ) {
return;
}
...
...
// Fetch the existing log or sets a blank array if not found
$log = get_option( 'option_logger', array() );
// Build the new entry for the log
$current_user = wp_get_current_user();
$new_item = array(
'option' => $option,
'old_value' => $old_value,
'value' => $value,
'user_id' => $current_user->ID,
'time' => current_time( 'timestamp' )
);
// Append the item to the top of the log
array_unshift( $log, $new_item );
// Remove the last item if the log is getting too long
if( count( $log ) > 200 ) {
array_pop( $log );
}
// Update the log, now it's been modified with the new entry
update_option( 'option_logger', $log );
}
add_action( 'updated_option', 'option_logger', 10, 3 );
add_action ( string $tag, callback $function_to_add,
int $priority = 10, int $accepted_args = 1 )
do_action ( string $tag, mixed $arg = '' )
ACTION REFERENCE
DO SOMETHING
https://developer.wordpress.org/reference/functions/add_action/
https://developer.wordpress.org/reference/functions/do_action/
PRiority
DO SOMETHING
do_action( 'hook' )
add_action(,,3)
add_action(,,2)
add_action(,,9)
add_action(,,1)
priority
DO SOMETHING
do_action( 'hook' )
add_action(,,1)
add_action(,,2)
add_action(,,3)
add_action(,,9)
Filters sit between the database and the browser
FILTER HOOK
MODIFY Something
add_FILTER() hooks a function onto the specified FILTER hook.
Apply_Filters() executes all THE hooked functions, returning modified content.
FILTER HOOK
MODIFY Something
When someone speaks, they should shout "Listen to me!" first, And then shout what they planned to say..
FILTER HOOK: Concept
FILTER HOOK: Concept
speak = Event when person speaks content = What the person is saying prepend_to_content = Filter to prepend 'Listen to me!' shout_message = Filter to make text uppercase
FILTER HOOK: Concept
SAY IT
PREPEND "LISTEN..."
function speak( $content ) {
echo apply_filters( 'the_speech', $content );
}
function listen_to_me( $content ) {
return 'Listen to me! ' . $content;
}
add_filter( 'the_speech', 'listen_to_me', 1 );
function shout_message( $content ) {
return strtoupper( $content );
}
add_filter( 'the_speech', 'shout_message', 2 );
Make uc
READ MORE
function xyz_excerpt_more( $more ) {
return '…';
}
add_filter( 'excerpt_more', 'xyz_excerpt_more' );
MODIFY Something
READ MORE

MODIFY Something
ADD BODY CLASS
MODIFY Something
function xyz_add_body_class( $classes ) {
$classes[] = 'site-' . sanitize_title( get_bloginfo( 'name' ) );
return $classes;
}
add_filter( 'body_class', 'xyz_add_body_class' );
SHOW EXCERPTS
MODIFY Something
/**
* Always show excerpt field.
* $hidden array An array of meta boxes hidden by default.
* $screen object WP_Screen object of the current screen.
*/
function xyz_show_excerpt( $hidden, $screen ) {
if ( 'post' == $screen->base ) {
$key = array_search( 'postexcerpt', $hidden );
if( !empty( $key ) ) {
unset( $hidden[ $key ] );
$hidden = array_values( $hidden );
}
}
return $hidden;
}
add_filter( 'default_hidden_meta_boxes', 'xyz_show_excerpt', 10, 2 );
FILTER REFERENCE
MODIFY Something
add_filter ( string $tag, callback $function_to_add,
int $priority = 10, int $accepted_args = 1 )
apply_filters ( string $tag, mixed $value )
https://developer.wordpress.org/reference/functions/add_filter/
https://developer.wordpress.org/reference/functions/apply_filters/
imagine a cms without hooks
imagine a cms without hooks
DON't BE AFRAID
open plugins and see how they work
just go write a plugin!
child themes...
...are not always the answer!
try A LITTLE PLUGIN WITH a hook
You want something different?
There's a hook for that!

@jamesbonham

There's a Hook For That
By James Bonham
There's a Hook For That
My presentation from WordCamp Denmark 2015. See the presentation here... http://wordpress.tv/2015/12/07/james-bonham-theres-a-hook-for-that/
- 2,743