WordPress.org

Make WordPress Core

Opened 5 years ago

Closed 3 years ago

#20920 closed defect (bug) (fixed)

Removing all hooks too aggressive if a priority number is given

Reported by: GeertDD Owned by: boonebgorges
Milestone: 4.2 Priority: normal
Severity: normal Version: 3.3.2
Component: Plugins Keywords: has-patch
Focuses: Cc:

Description

When you call remove_all_filters() or remove_all_actions() with a priority number (second parameter), all hooks get removed if none is found with the given priority.

Example:

<?php
global $wp_filter;

add_action('my_action', 'callback_foo');
add_action('my_action', 'callback_bar', 20);
var_dump($wp_filter['my_action']);

// At this point only callback_foo is left. Fine.
remove_all_actions('my_action', 20);
var_dump($wp_filter['my_action']);

// Exactly the same function call as above,
// but now all hooks bound to my_action are removed!
remove_all_actions('my_action', 20);
var_dump($wp_filter['my_action']);

Calling remove_all_filters() or remove_all_actions() with a priority should never remove hooks with another priority.

Attachments (3)

remove_all_filters.diff (549 bytes) - added by GeertDD 5 years ago.
20920.diff (1.3 KB) - added by valendesigns 3 years ago.
20920.2.diff (1.4 KB) - added by boonebgorges 3 years ago.

Download all attachments as: .zip

Change History (10)

#1 @SergeyBiryukov
5 years ago

  • Component changed from General to Plugins
  • Milestone changed from Awaiting Review to Future Release

#2 @mordauk
4 years ago

Seems sane to me.

#3 @nacin
4 years ago

  • Keywords needs-unit-tests added

@valendesigns
3 years ago

#4 @valendesigns
3 years ago

  • Keywords dev-feedback added; needs-unit-tests removed

The 20920.diff patch fixes the issue and includes supporting unit tests.

Cheers,
Derek

@boonebgorges
3 years ago

#5 @boonebgorges
3 years ago

  • Keywords dev-feedback removed
  • Milestone changed from Future Release to 4.2

20920.2.diff reverses the if statement to make it a bit easier to read, and gives the unit test a more accurate name.

#6 @nacin
3 years ago

Looks good.

#7 @boonebgorges
3 years ago

  • Owner set to boonebgorges
  • Resolution set to fixed
  • Status changed from new to closed

In 31014:

In remove_all_filters(), only remove callbacks that match the $priority parameter.

Props GeertDD, valendesigns.
Fixes #20920.

Note: See TracTickets for help on using tickets.