Opened 4 years ago

Last modified 35 hours ago

#9968 reopened defect (bug)

dynamically adding/removing filters breaks plugins

Reported by: Denis-de-Bernardy Owned by:
Priority: low Milestone: Future Release
Component: Plugins Version:
Severity: normal Keywords: has-patch needs-testing
Cc: azizur, frederic.demarle@…

Description

noticed this while doing something similar to this:

add_action('foo', 'bar1', 10);
add_action('foo', 'bar2', 20);

function bar1() {
  remove_filter('foo', 'bar1', 10);
}

in the above, bar2() doesn't get called. it is because of the usage of next()/current() to loop through filters.

attached patch uses a foreach loop instead, to make it work.

Attachments (2)

9968.diff (1.1 KB) - added by Denis-de-Bernardy 4 years ago.
9968.2.diff (1.2 KB) - added by Denis-de-Bernardy 4 years ago.
the two reset calls probably aren't needed either

Download all attachments as: .zip

Change History (12)

the two reset calls probably aren't needed either

oh, and, in case you're wondering why this is useful, it's to cope with output buffers, with an ob_start on the one hand side that removes itself (so as to not get triggered more than once), and an ob_flush on the other that removes itself for the same reason.

comment:2   dd324 years ago

  • Keywords needs-testing added; tested commit removed
  • Milestone changed from 2.8 to Future Release
  • Priority changed from high to low
  • Severity changed from blocker to normal

We've been down this route a few times over the years i think.

See #5338 for the latest such example.

Any changes to the Plugin API would require extensive testing as well.

  • Milestone Future Release deleted
  • Resolution set to wontfix
  • Status changed from new to closed

closing as wontfix then. the issue raised against the foreach in that other ticket is the same as mine, but the other way around. arbitration probably goes towards backwards compat.

Related: #10535

Related: #16245

  • Milestone set to Future Release
  • Resolution wontfix deleted
  • Status changed from closed to reopened
  • Cc azizur added

Related: #17817

Akismet is probably the most widely used plugin that does this (calls remove_action on a currently active filter) and it breaks a lot of other plugins that use that filter ( Bug report and trac ticket).

If this is difficult to fix outright, perhaps we could raise a debug error when remove_action is called on an active filter? At least that way plugin developers would get a warning when they're doing it wrong.

  • Cc frederic.demarle@… added

Just ran into the same issue of a plugin broken by another one due to this.

Note: See TracTickets for help on using tickets.