Last active
November 12, 2018 13:17
-
-
Save butlerblog/0d7843e9a2813f03b49ae95f9f8119ae to your computer and use it in GitHub Desktop.
How to use anonymous functions for simple filters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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/ | |
*/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'name_of_hook', 'name_of_function_to_use' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function name_of_function_called( $some_argument ) { | |
// do something... | |
return $something; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_filter( 'excerpt_length', 'excerpt_length_example' ); | |
function excerpt_length_example( $words ) { | |
return 15; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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