0

Code:

<?php do_action('post_footer'); ?>

Based on some reading, this appears to call a hook action. So, I went through all of the PHP files in the Editor and I don't find post_footer anywhere. I also noticed it call these hooks elsewhere, so it would be useful to know where these are stored. What files are these hook actions stored in?

What I searched for using is add_action in each file to discover the post_footer as one of the parameters, but never found anything for post_footer as an action. Where else would these action hooks be, if not in the php files?

  • there's no function named post_footer, think of hooks and actions as events – Tom J Nowell Aug 7 '16 at 20:09
3

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.

  • If you're going that far, maybe you could explain the difference between a filter and an action? Also priority and passing arguments - either through the calling of the action/filter or via the PHP handler using 'use'. – jgraup Aug 7 '16 at 20:35
  • Internally filters and actions use the same system, the only difference being the an action doesn't return a value, and a filter returns a value, which is then passed as the first parameter to the next function. Filters shouldn't do work as they're for filtering ( not actions/work ), and actions are for doing work ( not filtering/adjusting values ). Filters tend to be called more often than actions – Tom J Nowell Aug 7 '16 at 22:18
0

Anywhere within the code that WordPress runs to generate a page, do_action() and do_filter() define a hook. These hook definitions can be in core, in plugins or in themes.

I'm guessing that your do_action('post_footer') is within a theme, simply as I don't recognise it as a standard hook from core. The theme's author has put that there so that authors of plugins or child themes can use it to easily hook in their own code.

While there are guides to hooks around the web, sometimes you're best digging into core code to find the ones you need, as they do change with time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.