[wp-trac] [WordPress Trac] #43621: Introduce `add_action_once` and `add_filter_once` sugar.

WordPress Trac noreply at wordpress.org
Fri Mar 30 18:02:58 UTC 2018


#43621: Introduce `add_action_once` and `add_filter_once` sugar.
-------------------------------------------------+-------------------------
 Reporter:  soulseekah                           |       Owner:
     Type:  feature request                      |      Status:  new
 Priority:  normal                               |   Milestone:  Awaiting
Component:  Plugins                              |  Review
 Severity:  normal                               |     Version:
 Keywords:  has-patch has-unit-tests dev-        |  Resolution:
  feedback                                       |     Focuses:
-------------------------------------------------+-------------------------

Comment (by vortfu):

 I'm not the biggest fan of implementing something like this in the first
 place over explicitly add/removing callbacks as needed, but as is this
 patch only supports the use of one "once" callback (per priority), e.g.
 {{{
 add_filter_once( 'test_filter', '__return_false' );
 add_filter_once( 'test_filter', '__return_true' );

 // $wp_filter['test_filter']->callbacks
 //
 // array(1) {
 //   [10]=>
 //   array(3) {
 //     ["__return_false"]=>...
 //     ["_remove_filter_once"]=>...
 //     ["__return_true"]=>...
 //   }
 // }

 apply_filters( 'test_filter', null ); // true
 apply_filters( 'test_filter', null ); // true
 // ...
 apply_filters( 'test_filter', null ); // true
 // etc
 }}}

 `_remove_filter_once()` could be re-implemented as an anonymous func to
 ensure it's always added, but then you'd also have to check that the
 callback was actually added as well so other, non-once callbacks aren't
 accidentally removed ...

--
Ticket URL: <https://core.trac.wordpress.org/ticket/43621#comment:4>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list