Skip to content

Instantly share code, notes, and snippets.

@butlerblog
Last active November 12, 2018 13:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save butlerblog/0d7843e9a2813f03b49ae95f9f8119ae to your computer and use it in GitHub Desktop.
Save butlerblog/0d7843e9a2813f03b49ae95f9f8119ae to your computer and use it in GitHub Desktop.
How to use anonymous functions for simple filters
<?php
/**
* This gist is example code that goes with an article on how (and why/when) to use
* PHP anonymous functions. To view the entire post, go to:
* https://rocketgeek.com/basics/using-anonymous-functions-for-filters-and-actions/
*/
add_filter( 'name_of_hook', 'name_of_function_to_use' );
function name_of_function_called( $some_argument ) {
// do something...
return $something;
}
add_filter( 'excerpt_length', 'excerpt_length_example' );
function excerpt_length_example( $words ) {
return 15;
}
add_filter( 'excerpt_length', function( $words ) {
return 15;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment