WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#10334 closed feature request (wontfix)

Multiple Names for One Hook

Reported by: dcole07 Owned by: dcole07
Milestone: Priority: normal
Severity: trivial Version: 2.9
Component: Taxonomy Keywords:
Focuses: Cc:

Description

WordPress should be able to process more than one hook name at a single location. I know there shouldn't be a big need for it, but it helps if hook names are changed or if a 3rd party wants two hook names to be applies to the same location.

Example:
What if I wanted to attach a function to thematic_header, hybrid_header, prodigy_header, and theme_header. It's the same location in different themes. It'd be nice to tell WordPress these are the same, rather than defining 5 add_actions for each function you attach.

I'm willing to work on this enhancement. do_action and apply_filters would need to be modified and a new function would need to be created (What to call it?).

Attachments (2)

synonym_filters.php (2.5 KB) - added by dcole07 5 years ago.
Every synonym shares hooked functions with each other.
alias_filters.php (2.2 KB) - added by dcole07 5 years ago.
There is one master hook name and other alias names.

Download all attachments as: .zip

Change History (12)

comment:1 follow-up: dcole075 years ago

There wouldn't be a need for a new function, although it may make things nicer for people. If apply_filter and do_action looped through the array $wp_current_filter, then multiple hook names could be executed at once. The 'all' hook could add extra hook names into $wp_current_filter. The only other thing that would need to change is the array_pop( $wp_current_filter );

What does everyone think?

comment:2 in reply to: ↑ 1 dcole075 years ago

Replying to dcole07:

If apply_filter and do_action looped through the array $wp_current_filter...

That doesn't sort the function based on their priority. This is going to require the functions to be merged and sorted, plus repeats need to be left out. Maybe this isn't cracked up to be in core... too complex for too little advantage and too little use.

comment:3 dcole075 years ago

I created a prototype version that creates "synonym hook names." It's two functions, one for people to what hook names are synonyms and the other to merge the uncommon functions to be called. It still needs a little work and testing... Should this be added to core? It's about 30 lines of code.

comment:4 filosofo5 years ago

dcole07, the hook and filter API is so fundamental to WordPress that to change it you would need first to make a really compelling case. Posting code would also help.

comment:5 dcole075 years ago

  • Type changed from enhancement to feature request

I've attached a file with three functions. One makes a list of hook names that are synonyms, the other merges / distributes the functions that the synonym hook names have, and the last is a synonym function so people can make action hook names synonyms.

I'm changing this to a feature request because it no longer modifies any existing functions, but rather adds a couple of new functions.

comment:6 dcole075 years ago

Instead of calling this functionality a hook "synonym" it could be called a hook "alias". I used synonym because it was the first thing to come to mind.

The way I set up the code in synonym_filters.php was to create hooks that shared the same functions. This, however, might not be the most efficient way, since 1) there is an array in which all hooks with aliases list their aliases (hook1 list hook 2 and hook 2 lists hook1) and 2) The $wp_filter contains copies of each list of functions under each alias. It might be better to have a master hook name with other aliases listed under it. That way the alias list is smaller (easier to manage and change) and $wp_filter doesn't have multiple copies of the same functions. The only downside would be not being able to use aliases for do_action and apply_filters, but these could possible be worked out by checking who the alias belongs to.

dcole075 years ago

Every synonym shares hooked functions with each other.

dcole075 years ago

There is one master hook name and other alias names.

comment:7 dcole075 years ago

  • Keywords dev-feedback 2nd-opinion added

I've created two versions that are rough drafts. Synonyms shares all functions with hooks that are aliases, while Alias has one main hook name with other names that are aliases. While I prefer the synonym functionality, it might kill the efficiency if this is used by someone. The alias functionality might be more practical for more people than the synonym functionality.

Is this something that should be put into core? Which way should it be done? How should it be done? I'd like some reviewing and feedback.

comment:8 follow-up: azaozz5 years ago

-1. The proper way to fix this would be to standardize the hook name and location and add it to the Default and Classic themes. Also currently WordPress has over 1300 hooks. Even checking them for synonyms/aliases would add unneeded overhead.

comment:9 in reply to: ↑ 8 dcole075 years ago

  • Keywords dev-feedback 2nd-opinion removed
  • Milestone changed from 2.9 to Unassigned
  • Resolution set to wontfix
  • Status changed from new to closed

Replying to azaozz:

currently WordPress has over 1300 hooks. Even checking them for synonyms/aliases would add unneeded overhead.

Didn't even consider that. Thanks for bring up that point. A solution that doesn't involve using the 'all' hook may still be possible, but then the code runs the risk of not running for every hook. I think this has even changed my mind about the cost and benefits.

I'll standardize the themes that come with WordPress in the near future.

comment:10 Denis-de-Bernardy5 years ago

  • Milestone Unassigned deleted
Note: See TracTickets for help on using tickets.