[wp-trac] [WordPress Trac] #41961: Function to add an action to multiple hooks

WordPress Trac noreply at wordpress.org
Sat Sep 23 16:59:58 UTC 2017


#41961: Function to add an action to multiple hooks
--------------------------------+-----------------------------
 Reporter:  judahnator          |      Owner:
     Type:  feature request     |     Status:  new
 Priority:  normal              |  Milestone:  Awaiting Review
Component:  General             |    Version:  trunk
 Severity:  normal              |   Keywords:
  Focuses:  accessibility, rtl  |
--------------------------------+-----------------------------
 Hello!

 First Trac submission, so I hope I am in the right place!

 Several co-workers and I have a few helper functions we include in our
 projects, and one of them we use often enough that I thought it might be
 worth making a ticket for to see if it could be included in core.

 Its super straightforward. This is it:
 {{{#!php
 <?php
 /**
  * If you have a callback you want to run on multiple actions, pass them
 here.
  *
  * @param array $tags
  * @param callable $function_to_add
  * @param int $priority
  * @param int $accepted_args
  */
 function add_multiple_actions( array $tags, $function_to_add, $priority =
 10, $accepted_args = 1 ) {
         foreach ( $tags as $tag ) {
                 add_action( $tag, $function_to_add, $priority,
 $accepted_args );
         }
 }
 }}}

 An example of its use case would be something like saving a posts meta
 where you have a quick anonymous function you want to pass to both the
 'new_to_publish' and 'save_post' hooks.
 {{{#!php
 <?php
 add_multiple_actions( [ 'new_to_publish', 'save_post' ], function (
 $post_id ) {
         // foo, bar
 }
 }}}

 Of course you could make the callback an actual function and pass its name
 into multiple "add_action()" calls, but this is just another approach to
 it.

 Thoughts? Ideas? Improvements?

--
Ticket URL: <https://core.trac.wordpress.org/ticket/41961>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list