Make WordPress Core

Opened 19 months ago

Closed 18 months ago

Last modified 18 months ago

#54440 closed defect (bug) (fixed)

Make remove_filter param type more inclusive

Reported by: malthert's profile malthert Owned by: johnbillion's profile johnbillion
Milestone: 5.9 Priority: normal
Severity: normal Version: 2.2
Component: Plugins Keywords: has-patch
Focuses: docs Cc:


remove_filter/remove_action $callback type can be string/array too, not just callable, which is why it makes sense to make it more inclusive.

(e.g. if theme/plugin has code to remove_action for a function/action that might not exist, it would make no sense if we would have to check if it's callable before calling remove_action on it, since this logic is already there in remove_action)

Change History (10)

#2 @SergeyBiryukov
19 months ago

  • Focuses docs added

#3 follow-up: @SergeyBiryukov
19 months ago

  • Version changed from trunk to 2.2

Hi there, thanks for the PR!

It looks like this was introduced in [5142] / #3852 for WordPress 2.2 and adjusted in [34566] / #34032. Setting the version accordingly.

Just noting that there are currently 77 instances of @param callable in 22 files in core, so changing it just for these two functions seems inconsistent.

I think, while callable can technically be represented as a string or an array, it's not quite the same, as it's not just any string or array but specifically a callable function. So I think the current documentation makes sense, and changing it to callable|string|array could cause more confusion.

#4 @johnbillion
19 months ago

  • Keywords 2nd-opinion added

I understand the concern is that the string or array passed to this function does not have to be a valid callable, hence the suggestion to change the docblock to callable|string|array.

For example:

remove_action( 'foo', 'this_function_may_or_may_not_exist' );

The above is valid use of this function even though the $callback parameter is not a callable.

#5 in reply to: ↑ 3 @malthert
19 months ago

As John correctly says: for remove_filter the "callable" does not actually have to be callable. A string/array is fine (e.g. if your theme sets remove_action for a plugin that is not activated, the callback is NOT actually callable, but the code is still valid and runs without error)

For all other instances (e.g. add_action) it has to be callable, otherwise you will get a PHP error when it runs.

#6 @johnbillion
18 months ago

  • Keywords 2nd-opinion removed
  • Milestone changed from Awaiting Review to 5.9
  • Owner set to johnbillion
  • Status changed from new to reviewing

#7 @johnbillion
18 months ago

I pushed some additions to the PR. The following functions (and their WP_Hook counterpart methods) all accept a callback that doesn't have to be a valid callable:

  • remove_filter()
  • has_filter()
  • remove_action()
  • has_action()
  • _wp_filter_build_unique_id()
Last edited 18 months ago by johnbillion (previous) (diff)

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

18 months ago

#9 @johnbillion
18 months ago

  • Resolution set to fixed
  • Status changed from reviewing to closed

In 52300:

Plugins: Correct the documented allowable types for to the $callback parameter of various hook related functions.

These functions don't require the callback to be a valid callable, therefore array and string are also valid types for this parameter.

Props malthert

Fixes #54440

Note: See TracTickets for help on using tickets.