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.
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.
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).
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');
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 );
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 );
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 );
Note: If $num_args is not specified, then only $value will be passed.
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');
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)
http://codex.wordpress.org/Function_Reference/do_action
http://codex.wordpress.org/Function_Reference/add_action
http://codex.wordpress.org/Function_Reference/apply_filters
http://codex.wordpress.org/Function_Reference/add_filter
http://codex.wordpress.org/Plugin_API
http://codex.wordpress.org/Plugin_API/Action_Reference
http://codex.wordpress.org/Plugin_API/Filter_Reference
https://developer.wordpress.org/reference/
http://codex.wordpress.org/Plugin_API/Action_Reference/pre_get_posts
All pages above are hyperlinked.
And now you know enough to get into your own mischief with hooks!