I have a nifty helper function that I use in most of the WordPresss development I engage in. I figured I would post it here in case anyone happened to want to use it.

/**
 * Handy function for when you want one or more callbacks assigned to multiple action hooks.
 *
 * @param array $action_hooks
 * @param callable[] $callbacks
 */
function add_multiple_actions(array $action_hooks, callable ...$callbacks): void
{
    foreach ($action_hooks as $action_hook) {
        foreach ($callbacks as $callback) {
            add_action($action_hook, $callback);
        }
    }
}

The general idea is to help reduce code duplication. For example, lets say you have a field you want to add to a users profile. To do this you need two functions, registered to two different action hooks. Traditionally, you might need to do something like this:

function my_thing(): void
{
    // do the thing
}
function my_other_thing(): void
{
    // another thing
}
add_action('edit_user_profile', 'my_thing');
add_action('edit_user_profile', 'my_other_thing');
add_action('show_user_profile', 'my_thing');
add_action('show_user_profile', 'my_other_thing');

In my opinion this is not ideal. Those functions should not be exposed globally. I only want my plugin to use it. Not only that but it is creating a named object where one really is not needed. It would be much better to use anonymous functions, but doing that would require you to duplicate these functions for each hook you want to use.

Using the helper described above, you can do this instead:

add_multiple_actions(
    [
        'edit_user_profile',
        'show_user_profile'
    ],
    function(): void
    {
        // do the thing
    },
    function(): void
    {
        // another thing
    }
)

Many different variations of this have been submitted to WordPress Trac, going back as far as a decade. The WordPress devs don't really seem to want to implement this functionality, but I guess WordPress has a steriotype for holding the industry back for a reason.

Trac Sources:

It is important to mention that especially in themes it is best to keep internal functions out of the global scope as much as possible, and use anonymous functions in their place. Think about this from a security perspective.

Lets say your WordPress theme has a bunch of functions in the global scope that modify the output to the page. The more is left in the global scope the more attack vectors and potential for abuse exist. A bad actor could possibly use an exploit in conjunction with your funcitons to inject a malicious script into your users pages, or worse.

If you inject anonymous functions directly into WordPress, an attacker must work that much harder to exploit your code for their own purposes.

Definitely leave me feedback on Twitter and Mastodon, I would love to hear other folks thoughts on this!