Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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

<?php
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";
      break;
    case '_another_filter':
      echo "Called by another filter";
      break;
  }
}

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

share|improve this question
add comment

2 Answers

up vote 5 down vote accepted

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):

<?php
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);
  array_pop($wp_current_filter);
  return $value;
}

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

<?php
function current_filter() {
  global $wp_current_filter;
  return end( $wp_current_filter );
}
share|improve this answer
    
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 '10 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. –  MikeSchinkel Sep 1 '10 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 '10 at 10:00
    
@hakre: That is correct. OTOH using all really should be limited to debugging since it fires for (practically?) every hook. –  MikeSchinkel Sep 5 '10 at 19:51
add comment

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.

share|improve this answer
    
Hi @Nikolay - Good to see you here. Nice to meet you in Savannah! –  MikeSchinkel Sep 1 '10 at 8:59
    
Could you explain why using the same function for multiple hooks is less desirable? –  rsman Sep 1 '10 at 9:15
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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