WordPress.org

Make WordPress Core

Opened 7 months ago

#46712 new defect (bug)

Calling remove_filter aborts all filters on that tag from within the same class, despite different methods and priorities

Reported by: rogerlos Owned by:
Milestone: Awaiting Review Priority: normal
Severity: normal Version: 5.1
Component: General Keywords:
Focuses: Cc:
PR Number:

Description

Given this sample class and code:

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.

Change History (0)

Note: See TracTickets for help on using tickets.