[wp-trac] Re: [WordPress Trac] #5232: Get the current action and current filter name

WordPress Trac wp-trac at lists.automattic.com
Fri Oct 19 17:30:08 GMT 2007


#5232: Get the current action and current filter name
-------------------------+--------------------------------------------------
 Reporter:  tellyworth   |        Owner:  anonymous
     Type:  enhancement  |       Status:  new      
 Priority:  normal       |    Milestone:  2.4      
Component:  General      |      Version:           
 Severity:  normal       |   Resolution:           
 Keywords:  has-patch    |  
-------------------------+--------------------------------------------------
Comment (by mdawaffe):

 I recently wrote a plugin that filtered both the_content and comment_text.
 The filtration was dependent on what other filters were attached to those
 hooks.

 {{{
 if ( kses is attached to $this_hook ) {
   do this;
 } else {
   do something else;
 }
 }}}

 Since there's no way to know what {{{$this_hook}}} is in the above code, I
 was forced to write two functions: one with {{{$this_hook}}} hardcoded as
 the_content, and one doing the same with comment_text.

 Knowing what the current filter was would have made the code more
 efficient.

 As tellyworth also mentions, this knowledge can also be useful in
 debugging and unit testing.

 I'm also not a big fan of the global idea, but I like the feature.  We
 could use a static variable in a function that both sets and gets:

 {{{
 // Sets if passed one parameter
 // Gets if passed no parameters
 function wp_current_filter() {
   static $current = false;

   if ( func_num_args() )
     $current = func_get_arg(0);

   return $current;
 }

 function apply_filters( $tag, $string ) {
   ...
   // Set current filter
   wp_current_filter( $tag );
   ...
 }

 function my_filter( $string ) {
   // Get current filter
   $current_filter = wp_current_filter();
   ...
   return $string;
 }
 }}}

-- 
Ticket URL: <http://trac.wordpress.org/ticket/5232#comment:6>
WordPress Trac <http://trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list