After watching a Twitter discussion this morning (shown in the image below), Helen Hou-Sandi, a WordPress developer and core-committer mentioned a function I had never heard of that can be used for finding the name of the current action or filter hook. It is called current_filter() and is extremely useful. Check out the snippets below for examples.

Let’s say that you need to discover the name of the hook that is running, perhaps at a very specific time. The current_filter() function can be used for exactly that. While the examples below are very simple, they should illustrate the point well.

1
2
3
4
function pippin_show_current_filter() {
	echo 'Current action/filter: ' . current_filter(); exit;
}
add_action('template_redirect', 'pippin_show_current_filter');

When you load a page on your site, you will see:

Current action/filter: template_redirect
1
2
3
4
function pippin_show_current_filter() {
	echo 'Current action/filter: ' . current_filter(); exit;
}
add_action('pre_get_posts', 'pippin_show_current_filter');

Now when you load a page on your site, you will see:

Current action/filter: pre_get_posts
  1. Aj Clarke

    Oh ya, very useful 😉 Thanks!

Comments are closed.