7

Lots of hooks:

add_action('thesis_hook_after_comment', 'pj_time');
add_action('thesis_hook_after_comments', 'pj_time');
add_action('thesis_hook_after_comment_box', 'pj_time');
add_action('thesis_hook_after_comment_meta', 'pj_time');
add_action('thesis_hook_after_content', 'pj_time');
add_action('thesis_hook_after_content_area', 'pj_time');
add_action('thesis_hook_after_content_box', 'pj_time');
add_action('thesis_hook_after_footer', 'pj_time');
add_action('thesis_hook_after_header', 'pj_time');
add_action('thesis_hook_after_headline', 'pj_time');
// etc.

How can function pj_time() know which hook called it? Is there a global variable or something that tells me which hook we're on? So pj_time() could output something like: "The hook (name of hook here) rocks."

I don't know the order the hooks are called either, they're just in alphabetical order here. Otherwise I could use a counter to figure how many hooks we've done so far and determine the name of the hook that way.

So I want to know if it's possible to get the name of the hook. Thanks.

1
9
function pj_time () {
    echo current_filter();
}

Easier than I thought ;-)

http://codex.wordpress.org/Function_Reference/current_filter

3
  • +1 Another cool thing is that you can use that to remove functions and methods as well: remove_function( current_filter(), __FUNCTION__ ); and remove_function( current_filter(), array( $this, __FUNCTION__ ) );. Only problem is that you'd still have to extract the priority if it's not 10.
    – kaiser
    Oct 27 '13 at 3:58
  • Sure. We use it in our WCM Language switcher plugin.
    – kaiser
    Oct 28 '13 at 17:49
  • 1
    Dude, PJ Brunet, You're awesome. This little bit of code just saved me. I wanted to find a way to create an ACF repeater that you can choose the section of the page, and then made a bunch of action hooks to output the sections. But I couldn't figure out how to get the function to figure out the hook it was on. This did the trick perfectly. You're the king.
    – rgb_life
    Feb 13 '20 at 4:32

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.