Hooks/actions are better thought of as events.
When you run this code:
add_action( 'post_footer', 'toms_footer_stuff' );
You’re saying that when the post_footer
event happens, run the toms_footer_stuff
function.
These functions take the form of:
add_action( name_of_action, php_callable );
A PHP callable is something that can be called represented as an object. It can be any of these:
add_action( 'post_footer', 'toms_footer_stuff' ); // a function
add_action( 'post_footer', array( 'toms_class', 'toms_footer_stuff' ); // a static method on a class
add_action( 'post_footer', array( $toms_class, 'toms_footer_stuff' ); // a method on an object
add_action( 'post_footer', function() { echo "hello world"; } ); // an anonymous function
add_action( 'post_footer', $toms_closure ); // a Closure
When you call do_action('post_footer')
, you’re saying that the post_footer
event is happening, call all the things that have hooked into it. There’s no post_footer
function to call ( unless you define one yourself but that would just be a coincidence, it wouldn’t run unless you called add_action
).
post_footer
has more in common with a key in an array.
Filters are similar, except they take a value as their first function argument, and return it.
Internally they use the same system as actions, as such these 2 calls should do the same thing:
do_action( 'init' );
apply_filters( 'init', null ); // not recommended/tested
Actions are for doing work, filters are for adjusting or modifying data/content. If you do work in a filter, your site will slow down. Filters get called a lot more than actions and tend to happen multiple times ). Actions on the other hand tend to happen once at a specific time.