Temporarily add WordPress actions and filters

Quite regularly I need to use a WordPress filter or action for one little snippit of code and return it back to normal. I’ve done this with anonymous functions. It’s a quick and nifty way to get things done without writing things unnecessarily to the global scope.

function theExcerpt($length = 55, $more_text = '…') {
    $change_excerpt_length = function($old_length) use ($length) {
        return $length;
    $change_excerpt_more = function($old_more_text) use ($more_text) {
        return $more_text;
    add_filter('excerpt_length', $change_excerpt_length, 10);
    add_filter('excerpt_more', $change_excerpt_more, 10);
    remove_filter('excerpt_length', $change_excerpt_length);
    remove_filter('excerpt_more', $change_excerpt_more);


Leave a comment

Your email address will not be published.