When using Anonymous Functions in PHP < 5.3 I get this error:
Parse error: syntax error, unexpected T_FUNCTION
This is especially troublesome for add_filters. For instance, if I want to make a filter for a custom excerpt
The Function
function custom_excerpt($new_length = 20, $new_more = '...', $strip = false) {
add_filter('excerpt_length', function () use ($new_length) {
return $new_length;
}, 999);
add_filter('excerpt_more', function () use ($new_more) {
return $new_more;
});
$output = get_the_excerpt();
$output = apply_filters('wptexturize', $output);
$output = apply_filters('convert_chars', $output);
if(!$strip)
$output = '<p>' . $output . '</p>';
echo $output;
}
The function above accepts 3 parameters, lenth, read more, strip. You pass this function a new length such as '30' and it will give me an post excerpt of 30 words. The read more will replace the default [...]
with the functions default ...
. The point of this is to use the_excerpt()
and change the amount of words and remove the [...]
My question now is how can I pass my $new_length
variable to this filter in PHP < 5.3?
add_filter
can I do this while still passing my$new_length
?