WordPress.org

Make WordPress Core

Opened 2 years ago

Closed 7 months ago

#46712 closed defect (bug) (worksforme)

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: Priority: normal
Severity: normal Version: 5.1
Component: General Keywords:
Focuses: Cc:

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 (2)

This ticket was mentioned in Slack in #core by peterwilsoncc. View the logs.


7 months ago

#2 @markparnell
7 months ago

  • Milestone Awaiting Review deleted
  • Resolution set to worksforme
  • Status changed from new to closed

Hi @rogerlos, thanks for the report. We looked at this during a triage session on Slack today and we were unable to reproduce the behaviour you're describing in the current version of WP - the output is exactly as you said you were expecting it to be.

If you're still able to reproduce this in the latest trunk, please feel free to reopen it with any relevant details.

Note: See TracTickets for help on using tickets.