[wp-trac] [WordPress Trac] #33492: Slightly better add_filter
WordPress Trac
noreply at wordpress.org
Sat Aug 22 11:34:15 UTC 2015
#33492: Slightly better add_filter
-------------------------+------------------------------
Reporter: Iazel | Owner:
Type: enhancement | Status: new
Priority: normal | Milestone: Awaiting Review
Component: General | Version: trunk
Severity: normal | Resolution:
Keywords: | Focuses:
-------------------------+------------------------------
Comment (by Iazel):
Replying to [comment:1 dd32]:
> FWIW, the best way to handle this at present is to assign the anonymous
function to a variable which you later remove, the following code would
print `false, true, false` for example:
> {{{
> var_dump( has_filter( 'test' ) );
>
> add_filter( 'test', $function = function() {} );
>
> var_dump( has_filter( 'test' ) );
>
> remove_filter( 'test', $function );
>
> var_dump( has_filter( 'test' ) );
> }}}
>
> It seems to me, that allowing the closures to be named in this way could
result in conflicts with other plugins and functions. assigning it to a
variable and removing it later also encourages developers not to use
anonymous functions when they should be using a real function or method.
Sorry, but I don't see how this could raise more conflicts than using the
named functions, furthermore if you use the plugin domain, that should be
unique for every installed plugin as far as I know, hence it's impossible
to have a name conflict.
That's said, we could introduce a check to see if a name is already
present and raise an exception, but I discourage this approach because
then we should introduce a new parameter for the override or do it as we
already do.
I also don't see why anonymous function should be discouraged, this kind
of use case is the perfect one for them!
Obviously we can store it in a variable, but in the 99% of the case, that
variable will be out of scope for another plugin/template to replace it,
and that's the usual case.
And please, you should take note that this method doesn't replace the
actual workflow, therefore if you don't like anonymous functions, you can
continue to not use them.
--
Ticket URL: <https://core.trac.wordpress.org/ticket/33492#comment:2>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform
More information about the wp-trac
mailing list