[wp-trac] [WordPress Trac] #46712: Calling remove_filter aborts all filters on that tag from within the same class, despite different methods and priorities

WordPress Trac noreply at wordpress.org
Fri Mar 29 07:14:55 UTC 2019


#46712: Calling remove_filter aborts all filters on that tag from within the same
class, despite different methods and priorities
--------------------------+-----------------------------
 Reporter:  rogerlos      |      Owner:  (none)
     Type:  defect (bug)  |     Status:  new
 Priority:  normal        |  Milestone:  Awaiting Review
Component:  General       |    Version:  5.1
 Severity:  normal        |   Keywords:
  Focuses:                |
--------------------------+-----------------------------
 Given this sample class and code:

 {{{
 #!php
 class MyFilter {
     public function actions() {
         add_filter( 'get_the_excerpt', [ $this, 'a' ], 9 );
         add_filter( 'get_the_excerpt', [ $this, 'b' ], 10 );
         add_filter( 'get_the_excerpt', [ $this, 'c' ], 11 );
     }

     public function a( $html ) {
         $html .= ' [A]'
         return $html;
     }

     public function b( $html ) {
         remove_filter( 'get_the_excerpt', [ $this, 'b' ], 10 );
         $html .= ' [B]'
         return $html;
     }

     public function c( $html ) {
         $html .= ' [C]'
         return $html;
     }
 }

 $Test = new MyFilter();
 $Test->actions();
 }}}

 According to the documentation for `remove_filter` I would expect the
 output for a call to `get_the_excerpt` to be (assuming "Lorem ipsum dolor"
 is in `post_excerpt`:

 {{{
 Lorem ipsum dolor [A] [B] [C]
 }}}

 But it is, in fact:

 {{{
 Lorem ipsum dolor [A] [B]
 }}}

 If you move the `remove_filter` call to function "a", neither "b" nor "c"
 will be processed. If you move it to "c" all three will be processed.

 If you examine `$wp_filter` after calling `remove_filter` in "b" you will
 see that "c" is still assigned as a filter on `get_the_excerpt`, with its
 later priority. (It does not seem to matter what the priority numbers are
 for the three filters--1, 100, and 999 produce the same result.)

 In sum: It appears that once a filter within a class is removed from a
 tag, no further filters within that class will be called if they are also
 assigned to the same tag.

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


More information about the wp-trac mailing list