Hooks are a way for us to add our own code to customize default WordPress behavior. There're two types of hooks in WordPress:
- Action Hooks let us run our own code when a certain event takes place in the WordPress lifecycle.
- Filter Hooks let us get data from WordPress, modify it, and then return it back customized.
There are three fundamental functions to know about when working with action hooks:
<?php // index.php
do_action( 'before_footer' ); // create an action hook
?>
<?php // functions.php
add_action( 'before_footer', 'my_function' ); // hook in code
remove_action( 'before_footer', 'my_function' ); // unhook code
?>
- Wherever
do_action()
occurs, we have an action hook. The function doesn't do anything by itself, but it's the hook that we can use to run. - The
add_action()
function lets us add a function to an action hook. - To remove an action, we have to call
remove_action()
.
The Action Lifecycle refers to the order and relationship od do_action()
calls. Check Plugin API/Action Reference | WordPress Codex for more information.
There're several ways to explore action hooks:
- Use the global variables
$wp_actions
and$wp_filter
to get a list of all the action hooks and filters.$wp_actions
is an array of all the action hooks that have been registered.$wp_filter
is an array of all the filters that have been registered.
- Use the
r-debug.php
plugin to get a list of all the action hooks and filters.- Add
require( dirname( __FILE__ ) . '/lib/r-debug.php' );
to thefunctions.php
file.
- Add
- Use the Debug Bar and Debug Bar Actions and Filters plugins to get a list of all the action hooks and filters.
- Use the Simply Show Hooks plugin to get a list of all the action hooks and filters.