[wp-trac] [WordPress Trac] #33492: Slightly better add_filter

WordPress Trac noreply at wordpress.org
Fri Aug 21 17:10:34 UTC 2015


#33492: Slightly better add_filter
-------------------------+-----------------------------
 Reporter:  Iazel        |      Owner:
     Type:  enhancement  |     Status:  new
 Priority:  normal       |  Milestone:  Awaiting Review
Component:  General      |    Version:  trunk
 Severity:  normal       |   Keywords:
  Focuses:               |
-------------------------+-----------------------------
 I want to suggest a small change for `add_filter`, adding a new parameter
 to explicitly define the `$idx` used.

 {{{
 #!php
 <?php
 function add_filter( $tag, $function_to_add, $priority = 10,
 $accepted_args = 1, $idx = null ) {
         global $wp_filter, $merged_filters;

         if($idx === null)
                 $idx = _wp_filter_build_unique_id($tag, $function_to_add,
 $priority);
         $wp_filter[$tag][$priority][$idx] = array('function' =>
 $function_to_add, 'accepted_args' => $accepted_args);
         unset( $merged_filters[ $tag ] );
         return true;
 }
 }}}

 I've just made `$idx` explicit and for obvious retrocompatibility reasons
 this have to be at the end.
 Why we should do this small change? Well, this will help a lot and will
 enable a full use of anonymous functions:

 {{{
 #!php
 add_action('init', function(){ /* some code */ }, 10, 0, 'plugin_domain');
 add_action('the_title', function($t){ return $t; }, 10, 0,
 'plugin_domain');
 }}}

 Please notice that I've used the domain name as the `$idx`, therefore we
 can:
 {{{
 #!php
 remove_action('init', 'plugin_domain');
 }}}

 It would be easier and more efficient if we all follow the convention of
 set `$idx = $domain_name`, hence if anyone want to remove a specific hook
 introduced by some plugin, doesn't need to read the code to know it.
 Also, if we use anonymous functions instead of named, the namespace
 pollution will decrease by a lot and performance should increase (it
 doesn't even have to calculate the `$idx`).
 It will also enable the override of specific hook. Today we need to remove
 the hook we want to override and add another one, because we cannot
 redifine a named function. However, with this change all we have to do is:

 {{{#!php
 add_action('init', function(){ /* new code */ }, 10, 0,
 'plugin_to_override');
 }}}

 A syntactical sugar will be great:
 {{{#!php
 function new_add_action($hook, $idx, $callback, $priority = 10,
 $args_count = 1) {
    return add_filter($hook, $callback, $priority, $args_count, $idx);
 }
 }}}

 I don't know what the name should be yet, but introducing it is a good
 compromise between retrocompatibility and good API; in years the first
 version should be deprecated... Something like that.

 All in all, a really small change can simplify a good amount of things.
 I don't really know if this is the correct way to propose this change, but
 I still haven't found the equivalent of git's pull request on SVN, sorry
 for that `^^"`

--
Ticket URL: <https://core.trac.wordpress.org/ticket/33492>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list