WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

#26704 closed feature request (duplicate)

Multiple actions when using add_action

Reported by: sigurdbergsvela Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.9
Component: Plugins Keywords:
Focuses: Cc:

Description

I would like the possibility of naming several actions when using add_action
I propose a space separated list. This will then be similar to jQuery’s on/bind/live.

add_action(‘action_one action_two action_three’, ‘function_name’);

Its true that, since you can just give it a function name, it is not that big a deal to just call
add_action several times, but since php >= 5.3, php added anonomous function. Using anonomous function is create, if you don’t actually want to create a function for the code you want to be executed on an action.
I, personally, before PHP 5.3 used insanely long function names in these situstions to avoid conflicts, but thats just ugly.

Thats why i propose this

add_action(‘one two three four’, function(){});

Attachments (1)

multiple-actions.patch (664 bytes) - added by sigurdbergsvela 5 years ago.
Patch that implements this feature

Download all attachments as: .zip

Change History (3)

#1 @sigurdbergsvela
5 years ago

  • Component changed from General to Plugins

@sigurdbergsvela
5 years ago

Patch that implements this feature

#2 @dd32
5 years ago

  • Milestone Awaiting Review deleted
  • Resolution set to duplicate
  • Status changed from new to closed

Two main issues:

  1. We currently allow actions to have spaces in them, although we don't generally advise it, an action can be made up of any valid PHP string.
  2. The add_action/add_filter set of functions are called hundreds of times per page load, adding an explode() to that function will make running the function more expensive without any gained benefit for almost every existing use of the functions.

A few general suggestions:
*. Closures in hooks is kind of bad, you're completely unable to remove the function from the hook, and in the event that all functions are removed from the hook by a plugin, it can't re-add the function. Leads to a harder life for those performing deep customizations of existing plugins

  • If you use a PHP class (or namespace if you're into that kind of thing), you get around the need to have long function names, filter_{$filter_name}() is a common method name in a lot of plugin classes.
  • A similar proposal to pass an array of actions has also been suggested: #14280 - this also fits the expensive with little benefits case mentioned above.
Note: See TracTickets for help on using tickets.