Everything you wanted to know about WordPress Hooks

Amanda Giles

Feb 4, 2015

Seacoast NH WordPress Meetup

https://slides.com/amanda_giles/wphooks

But were afraid to ask!

We'll cover...

  • What is a hook?
  • Why use hooks?
  • Types of hooks
  • How do they work?
  • Syntax
  • Examples
  • Creating custom hooks
  • Checking for and removing hooks
  • Reference Material

What is a hook?

A hook is an "event" which allows for additional code to be run when it occurs.

 

One or more functions can be associated with a hook and they will all run when the hook is triggered.

 

Those functions can be given precedence to control the order in which they run.

Why use hooks?

Hooks are placed within WordPress core, plugins, and themes to allow customization by developers without direct edits of the code.

 

Hooks are the proper way to alter the default behavior of code which is not yours to edit.

Types of hooks

Action hooks allow you to run code at a certain point within the code.

Examples in WP core include initialization, before main query is run, header or footer of a page/post.

 

Filter hooks allow you to alter data, content, parameters. A filter hook is passed information to filter and returns it altered (or not).

Examples in WP code include displaying content, page/post title, pre-saving content (admin).

How do action hooks work?

Action hooks are triggered by do_action()

do_action('wp_logout');

 

And they are "hooked" into using add_action()

function abc_clear_data() {
	$_SESSION['customerid'] = '';
	$_SESSION['cart'] = array();
}
add_action('wp_logout','abc_clear_data');

Syntax of action hooks

Defined with do_action() with a hook name and optional extra arguments to pass to "hooking" function(s).

do_action($hook);
do_action($hook, $arg_a, $arg_b, $arg_c);

 

And they are "hooked" into using add_action()

add_action( $hook, $function_name, $priority, $num_args );
  • $hook : the name of the action hook
  • $function_name : name of the function to hook in
  • $priority : Specify order precedence, default = 10  (Optional)
  • $num_args : (Optional) Specify # of arguments, default =1  (Optional)

How do filter hooks work?

Filter hooks are triggered by apply_filters()

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

 

And they are "hooked" into using add_filter()

function abc_excerpt_length( $length ) {
	return 20;
}
add_filter( 'excerpt_length', 'abc_excerpt_length', 999 );

Syntax of filter hooks

Defined by apply_filters() with a hook name, a value, and optional extra arguments to pass to "hooking" function(s). Note:The value returned by apply_filters must be stored.

$newvalue = apply_filters( $hook, $value );
$newvalue = apply_filters( $hook, $value, $arg_a, $arg_b );

 

And they are "hooked" into using add_filter()

add_filter( $hook, $function_name, $priority, $num_args );
  • $hook : the name of the action hook
  • $function_name : name of the function to hook in
  • $priority : Specify order precedence, default = 10  (Optional)
  • $num_args : (Optional) Specify # of arguments, default =1  (Optional)

 

Note: If $num_args is not specified, then only $value will be passed.

Let's see Hooks in action

Creating custom hooks

When creating themes and plugins, you can create your own hooks by placing do_action() and apply_filters() calls at appropriate points in your code.

 

Be sure to make the names unique by adding a prefix related to your theme or plugin to the name of the hook. This will help to avoid conflicts with other hooks (although it's not foolproof).

 

do_action('myplugin_action_time');

apply_filters('myplugin_post_info');

Checking for and removing hooks

Here are some other functions related to action and filter hooks:

has_filter( $hook, $function_to_check )
has_action( $hook, $function_to_check )
current_filter()
doing_filter( $hook = NULL)
doing_action( $hook = NULL) 
remove_filter( $hook, $function_to_remove, $priority )
remove_action( $hook, $function_to_remove, $priority )
remove_all_filters( $hook, $priority)
remove_all_actions( $hook, $priority)

Reference Material

What we Learned

  • What hooks are
  • Why and how to use them
  • The difference between action and filter hooks
  • How to create our own hooks
  • Checking for and removing hooks
  • Where to find more information

 And now you know enough to get into your own mischief with hooks! 

Thank you!

Made with Slides.com