Make WordPress Core

Opened 6 years ago

Closed 6 years ago

#41961 closed feature request (duplicate)

Function to add an action to multiple hooks

Reported by: judahnator's profile judahnator Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.9
Component: Plugins Keywords:
Focuses: Cc:

Description

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
/**
 * 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
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?

Change History (1)

#1 @SergeyBiryukov
6 years ago

  • Component changed from General to Plugins
  • Focuses accessibility rtl removed
  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Hi @judahnator, welcome to WordPress Trac!

Thanks for the ticket, we're already tracking this enhancement in #34309.

Note: See TracTickets for help on using tickets.