Sometimes we’d appreciate a possibility to add another parametr to function called as a filter callback. WordPress „prefills“ the callbacked function with it’s own, pre-defined, parameters and we often have to look for workarounds (global variables, transitions etc.). With PHP 5.3, anonymous function and closures, we don’t have to look for this anymore. Have a look on this simple snippet:
$my_var = 10; //can be just anything... add_filter( 'the_content', function( $content ) use ($my_var){ my_real_callback_function( $content, $my_var ); }, 10, 1 ); my_real_callback_function( $content, $my_var ){ ... //do whatever you want to do with the content and you variable }
If you’d appreciate a tip for removing filter with anonymous function, checkout my another post.