[wp-hackers] anonymous functions for do_action and apply_filters
scribu
mail at scribu.net
Mon Dec 20 20:59:29 UTC 2010
>
> add_filter('the_title', function($title) { return '<b>'. $title. '</b>';
> });
>
Yes, this is already possbile in WP 3.0:
http://core.trac.wordpress.org/ticket/10493
On Mon, Dec 20, 2010 at 10:49 PM, Nathaniel Taintor <
goldenapplesdesign at gmail.com> wrote:
> I think its generally frowned upon, because if you add an anonymous
> function, then you have no way of removing it, other than calling
> remove_all_filters.
>
A valid concern; discretion is advised.
There are also concerns about memory leaks caused by using lambda functions
> in php. The add_filter code you cited would create a new function every
> time
> it was called, which may not be a big deal in your case (you're not likely
> to trigger the title filter more than 20-50 times in any one page), but
> would still be slightly less efficient than defining the function once.
>
That's a common misconception. PHP will evaluate it only once. It will
internally create separate instances of Closure objects if there is a 'use'
clause.
More information about the wp-hackers
mailing list