You are on page 1of 24

INTRODUCTION TO

WORDPRESS HOOKS

build create WORDCAMP GRAND RAPIDS


HI.
I’M IAN, NICE TO MEET YOU.

Designer, developer,
marketer, shit-talker, etc.

Co-owner build/create studios

@buildcreate
@wilsonography
buildcreate.com

build create WORDCAMP CHICAGO


MY SELFISH GOALS FOR YOU

I want you to walk away from this feeling a little less


hacky, a little more informed, and most of all inspired.

I want you to have an “Ah ha!” moment, where suddenly


your understanding of WordPress becomes that much
clearer, and you can’t wait to get out of this room to try
out your new knowledge.

Here goes nothing…

build create WORDCAMP GRAND RAPIDS


L E T ’ S TA L K A B O U T H O O K S

“Hooks enable us to literally hook into parts of the


WordPress page lifecycle to retrieve, insert, or modify
data, or they allow us to take certain actions behind the
scenes.”

- T O M M C FA R L I N @ T U T S P L U S

build create WORDCAMP GRAND RAPIDS


A WORLD WITHOUT HOOKS

build create WORDCAMP GRAND RAPIDS


IT’S PRETTY DAMN HACKY

• Including extra PHP files for ajax


requests

• Hacking into plugins to change


display/functionality

• Hacking *gasp* WP core files

• Hacking theme files (when you


should be using a child theme)

• Hackity hack hack hack.

build create WORDCAMP CHICAGO


F I LT E R S ACTIONS

• Applying changes to • Hooks your functions into


content/data an action in WP

• Uses functions: • Uses functions:

• apply_filters() • do_action()
This is the hook. This is the hook.

• add_filter() • add_action()
This is what you use to tie into This is what you use to tie into
the hook. the hook.

build create WORDCAMP CHICAGO


Filters and Actions are nearly the same thing in WordPress,
almost identical syntax, capabilities, and limitations.

The difference is how you use them.

build create WORDCAMP GRAND RAPIDS


WHEN SHOULD WE USE THESE
L O V E LY H O O K S ?

F I LT E R S ACTIONS

• Change something you’re • Tying into existing WP processes,


pulling out of WP- the like sending email, saving a post,
content, a media file, etc. saving a comment, etc.

• Add an action to your plugin/


• Change something you’re
theme to allow other developers
putting into the WP database to manipulate it without hacking

build create WORDCAMP CHICAGO


AKA ALL THE DAMN
TIME. IT’S MAGIC.

build create WORDCAMP CHICAGO


SO, LETS MAKE SOME MAGIC…

build create WORDCAMP GRAND RAPIDS


A N AT O M Y O F A H O O K
Any additional
Your function you arguments you
want to run when want to pass to
the filter is applied the function

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


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

The name of the Lower numbers =


filter to hook into earlier execution
of your filter

build create WORDCAMP GRAND RAPIDS


SOME NITTY GRITTY NOTES
• Usually a filter comes with one argument by default- the content that you’re going to be
modifying. Some filters accept additional arguments as specified in the matching
“apply_filter” call. Make sure you’re aware of these when you use the filter.

• Though you can pass all kinds of arguments into your filter function, the only thing you
get back is the value of what you’re filtering. So for example if you’re using
‘the_content’ filter, and you have your function accept the post ID as well as the default
content argument, all you can safely return at the end of your function is the content.

• Actions don’t return any data, only true. Always true. Deal with it.

• Actions also allow you to pass “all” as the hook name to tie into every hook. Yikes.

• You can pass a class method like so: array( 'My_Class', ‘my_class_method’ ).
This is especially useful in plugin development when ofttimes your plugin has several
class methods.

build create WORDCAMP GRAND RAPIDS


U P N E X T: A S U P E R D U P E R S I M P L E E X A M P L E

build create WORDCAMP GRAND RAPIDS


function my_filter_function($content) {
$output = '<div>'.$content.'</div>';
return $output;
}

add_filter(‘the_content’, ‘my_filter_function’, 10);

build create WORDCAMP GRAND RAPIDS


This variable comes
through from the
matching apply_filter()

function my_filter_function($content) {
$output = '<div>'.$content.'</div>';
return $output;
}

Your function you want to


run when the filter is applied

add_filter(‘the_content’, ‘my_filter_function’, 10);

The name of the Lower numbers = earlier


filter to hook into execution of your filter

build create WORDCAMP GRAND RAPIDS


DEFINING YOUR HOOKS
The value you
want to modify
with the filter

apply_filter( $tag, $value, $arg );

The name of the One or more additional arguments


filter to hook into passed to the filter function

do_action( $tag, $arg);


See? The only difference is that we aren’t
passing a value to modify.

build create WORDCAMP GRAND RAPIDS


EXAMPLE HOOKS
• save_post - Action: runs whenever a post or page is created/updated. Useful for saving custom
meta information.

• add_meta_boxes - Action: used to add meta boxes to the WordPress edit screen. You’d then
probably wind up using the save_post action to validate the data before saving.

• wp_enqueue_scripts - Action: used to add styles and scripts to the front end. Used a lot in themes
and plugins. There’s also an admin and login version for adding styles and scripts to those areas.

• the_content, the_title, etc - Filter: used to modify the content and title respectively. There are also
filters to modify these in the editor on the backend of WordPress.

• wp_authenticate_user - Filter: used to tie into the authentication process and run your own
authentication function on the user’s login form submission.

• body_class - Filter: say you want to add some classes to the body based on various conditions
(assuming you’re using the body_class function in your template), you would use this hook in your
functions.php to handle that.

build create WORDCAMP GRAND RAPIDS


The best way to learn is to look at other people’s code and in the
WordPress Codex. See how they use it, how it comes together,
and of course try it yourself!

Experiment!

build create WORDCAMP GRAND RAPIDS


M E O W W E ’ R E TA L K I N !

build create WORDCAMP GRAND RAPIDS


LETS GET TECHNICAL

Where does all of this


fit into the WP loading
process?

build create WORDCAMP CHICAGO


NOW YOU’RE THINKING WITH HOOKS

• Work WITH plugins as the author


intended instead of hacking them!

• GTFO WP core files!

• Build better themes, child themes,


and plugins!

• ????

• Profit!

build create WORDCAMP CHICAGO


THANKS FOR COMING!

build create WORDCAMP GRAND RAPIDS


RESOURCES

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

• http://code.tutsplus.com/articles/the-beginners-guide-to-
wordpress-actions-and-filters--wp-27373

• http://www.zell-weekeat.com/wordpress-actions-and-filters/

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

build create WORDCAMP CHICAGO

You might also like