[wp-trac] [WordPress Trac] #25241: Add $tag array handling for running multiple filters/actions at once w/ same value and arguments

WordPress Trac noreply at wordpress.org
Fri Sep 6 17:16:59 UTC 2013


#25241: Add $tag array handling for running multiple filters/actions at once w/
same value and arguments
-------------------------+--------------------------------------
 Reporter:  sc0ttkclark  |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  Plugins      |    Version:  3.6
 Severity:  normal       |   Keywords:  dev-feedback needs-patch
-------------------------+--------------------------------------
 Example of use case that is growing in plugins:

 {{{
 $value = apply_filters( 'my_plugin_filter_' . $some_var, $value,
 $some_var, $another_var, $and_another );
 $value = apply_filters( 'my_plugin_filter', $value, $some_var,
 $another_var, $and_another );
 }}}

 I myself am using, and I'm seeing a lot of cases where this specific
 method of filtering is used, providing developers with an easy way to add
 specificity to the filters/actions they want to hook into, instead of
 adding a catch-all filter and trying to add the extra code to check
 $some_var all over the place.

 Birds do it, bees do it, even the WP core does it:

 {{{
         do_action( "save_post_{$post->post_type}", $post_ID, $post,
 $update );
         do_action( 'save_post', $post_ID, $post, $update );
         do_action( 'wp_insert_post', $post_ID, $post, $update );
 }}}

 So examples here on simplified usage would be:

 {{{
         do_action( array( "save_post_{$post->post_type}", 'save_post',
 'wp_insert_post' ), $post_ID, $post, $update );
 }}}

 And from my plugin example above, simplified:

 {{{
 $value = apply_filters( array( 'my_plugin_filter_' . $some_var,
 'my_plugin_filter' ), $value, $some_var, $another_var, $and_another );
 }}}

 This wouldn't require a *huge* change, in every filter/action function we
 add this support to for the $tag var, we simply add this to the top of the
 function itself.

 For filters:

 {{{
 if ( is_array( $tag ) ) {
     $args = get_func_args();

     foreach ( $tag as $t ) {
         $args[ 0 ] = $t; // set $tag
         $args[ 1 ] = $value; // pass filtered $value

         $value = call_user_func_array( 'apply_filters', $args );
     }

     return $value;
 }
 }}}

 For actions:

 {{{
 if ( is_array( $tag ) ) {
     $args = get_func_args();

     foreach ( $tag as $t ) {
         $args[ 0 ] = $t; // set $tag

         call_user_func_array( 'do_action', $args );
     }

     return;
 }
 }}}

 If developers like this idea, I will code it up and get a patch out
 quickly.

--
Ticket URL: <http://core.trac.wordpress.org/ticket/25241>
WordPress Trac <http://core.trac.wordpress.org/>
WordPress blogging software


More information about the wp-trac mailing list