Passing an anonymous function to multiple actions/filters

I wrote a quick set of helpers to assist in passing an anonymous function to multiple actions/filters as this has been an annoyance of mine since being introduced to anonymous functions and preferring them wherever possible. This is useful in scenarios such as wp_ajax and wp_ajax_nopriv.

Under the hood, add_action() is simply a call to add_filter(), so we are able to have add_actions() call our new add_filters() just as WordPress does in it’s core.

collect() accepts an array or string, and if it receives a string, converts it to an array; so type checking our $filters is not necessary.

helpers.php

/**
 * Pass multiple filters to a callback.
 *
 * @param  mixed   $filters
 * @param  mixed   $callback
 * @param  integer $priority
 * @param  integer $arguments
 * @return true
 */
function add_filters($filters, $callback, $priority = 10, $arguments = 1)
{
    collect($filters)->each(function ($filter, $index) use ($callback, $priority, $arguments) {
        add_filter(
            $filter,
            $callback,
            (int)is_array($priority) ? $priority[$index] : $priority,
            (int)is_array($arguments) ? $arguments[$index] : $arguments
        );
    });

    return true;
}

/**
 * Pass multiple actions to a callback.
 *
 * @param  mixed   $actions
 * @param  mixed   $callback
 * @param  integer $priority
 * @param  integer $arguments
 * @return true
 */
function add_actions($actions, $callback, $priority = 10, $arguments = 1)
{
    return add_filters($actions, $callback, $priority, $arguments);
}

Usage

// Simple Usage
add_filters(['wp_ajax_test', 'wp_ajax_test_nopriv'], function () {
    wp_die();
});

// Passing multiple priority / argument values
add_filters(['wp_ajax_test', 'wp_ajax_test_nopriv'], function () {
    wp_die();
}, [10, 12], [1, 2]);
8 Likes

Hey, interesting idea!

I’ve played with wrappers for add_filter before but I’ve been shying away from them lately as you lose the static analysis with PHPStorm’s WordPress integration. I find it’s worth the extra cost to keep that utility of being able to jump to the hook’s invocation.

I’m curious what other hooks you’ve used this with where you would want to pass the same callback to all of them. The only other ones I can think of off the top of my head would be (wp|admin|login)_head.

I like your implementation; my only criticism here is that the priority and args arrays are a bit cryptic, especially if you had a few more on there. I would imagine having different values for each would be rather unlikely though since you’re hooking the same callback?

Thanks!

The priority and args is indeed a bit cryptic and I can’t really imagine a use-case where you’d need it. I more-so just added it out of good measure.

add_filter() is the only hook I’ve done this with and have only used it with wp_ajax(_nopriv) and create/edit/delete_taxonomy to flush my rewrite rules when doing things with a custom taxonomy due to the complexity of my current rewrite rules on the project I’m working on. So more or less, rather basic use-cases thus’ far.