Opened 12 years ago
Closed 11 years ago
#24431 closed enhancement (wontfix)
Boolean shorthand for add_filter
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Priority: | normal | |
Severity: | normal | Version: | |
Component: | Plugins | Keywords: | |
Focuses: | performance | Cc: |
Description
Callbacks like __return_false or __return_true are often passed to add_filter
to negate a filter or flip an option. This creates uneeded function calls. A potential performance-saver is to enable add_filter
to support bools being passed as callbacks. add_filter
doesn't validate the callback anyway, so no change would be needed there. A check could be done as apply_filters runs:
$value = is_bool($the_['function']) ? $the_['function'] : call_user_func_array( $the_['function'], array_slice($args, 1, (int) $the_['accepted_args']) );
3quals checks like false === $fn || true === $fn
could be slightly faster. My hypothesis is that greater savings would be made from avoiding calls to array_slice
, call_user_func_array
, and the callback itself. A minor secondary benefit is that storing bools in the hash rather than strings like __return_false
should reduce memory consumption.
It of course would also add syntactic sugar like:
add_filter('use_default_gallery_style', false)
Change History (3)
#2
@
12 years ago
@toscho Priorities could work in one of two ways. I'm open to either. 1 is simpler. 2 is more capable:
- Subsequent (later priority) callbacks run as normal.
false
would work just like__return_false
does now. - Booleans provide a way to
break
from the loop. Subsequent (later priority) callbacks would not fire.
How would priorities be handled?