Is this a good example of usage of current_filter()?

add_filter("_my_filter", "common_function");
add_filter("_another_filter", "common_function");
function common_function(){
  $currentFilter = current_filter();
  switch ($currentFilter) {
    case '_my_filter':
      echo "Called by My Filter";
    case '_another_filter':
      echo "Called by another filter";

So I am guessing current_filter() is used to get the name of the filter for which the current execution is happening?

2 Answers 2


Hi @Raj Sekharan:

Looks good to me, but is wanting to know the current usage really your question or do you want to understand where current_filter() gets it's information from?

If the latter, here's basically what happens in all the different hook processing functions, e.g. do_action(), apply_filters(), do_action_ref_array(), apply_filters_ref_array() (greatly simplified, of course):

function <process_hook>($hook, $value) {
  global $wp_filter, $wp_current_filter;
  $wp_current_filter[] = $hook;  // "Push" the hook onto the stack.
  $value = call_user_func($wp_filter[$hook]['function'],$value);
  return $value;

Then all that current_filter() does is retrieve the last hook "pushed" onto the global wp_current_filter array, i.e.:

function current_filter() {
  global $wp_current_filter;
  return end( $wp_current_filter );
  • I read the source and figured the pushing and popping part out. I was wondering why would we need this function. Must be some good use case. I thought may be to help discover which hook is being processed when using the same filter function is hooked onto both.
    – rsman
    Sep 1, 2010 at 8:16
  • Hi @Raj Sekharan - In that case you already answered your own question. There are some hooks where it's easier to route to a common function but sometimes you need to do something slightly differently depending on the hook. Here's an example of many hooks routing to one function (stackoverflow.com/questions/3472334/#3474234) and if there were a need to know which hooks them the current_filter() function would be useful indeed. BTW, I didn't know it existed until I read you question but I immediate saw how it could be useful in special cases. Sep 1, 2010 at 8:58
  • Sidenote: $wp_current_filter does not work for the "all" hook, so you need to add your own logic to find out wether your hook function has been triggered by 'all' or another (concrete) hook. $wp_current_filter will always contain the concrete hook name, never 'all'.
    – hakre
    Sep 5, 2010 at 10:00
  • @hakre: That is correct. OTOH using all really should be limited to debugging since it fires for (practically?) every hook. Sep 5, 2010 at 19:51

In general—yes, this is a valid usage. If I were you I would pass different functions to the different filters and abstract the common parts in other function(s).

This way any of your function will do exactly one thing.

  • Hi @Nikolay - Good to see you here. Nice to meet you in Savannah! Sep 1, 2010 at 8:59
  • Could you explain why using the same function for multiple hooks is less desirable?
    – rsman
    Sep 1, 2010 at 9:15

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.