0

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?

7
  • What are you actually trying to do here? Can you please edit your question to better explain what you're trying to accomplish? Commented Nov 21, 2013 at 18:59
  • Updated my question.
    – Howdy_McGee
    Commented Nov 21, 2013 at 19:31
  • 1
    stackoverflow.com/questions/6412032/…
    – s_ha_dum
    Commented Nov 21, 2013 at 19:31
  • @s_ha_dum - that makes sense, so it's the PHP version I'm using. The link you suggested says to use a separate function, but with add_filter can I do this while still passing my $new_length?
    – Howdy_McGee
    Commented Nov 21, 2013 at 19:34
  • 1
    "...but with add_filter can I do this while still passing my $new_length?" - this would make a fantastic question for WPSE, and one even better than what you have currently. Would you mind editing your question accordingly? Commented Nov 21, 2013 at 19:35

1 Answer 1

1

You could try something like the second option, but I think it would be overkill. Instead, just write your own custom analog to wp_trim_excerpt() (the function that applies the excerpt_length and excerpt_more filters to the excerpt), like so:

function custom_excerpt( $new_length = 20, $new_more = '...', $strip = false ) {
    // Start with the content
    $text = get_the_content('');

    // Do stuff to it
    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]&gt;', $text);

    // Use custom values
    $excerpt_length = $new_length;
    $excerpt_more = $new_more;
    $text = wp_trim_words( $text, $excerpt_length, $excerpt_more );

    // Strip?
    if ( ! $strip ) {
        $text = '<p>' . $text . '</p>';
    }

    // Output
    echo $text;
}

Overkill method:

function custom_excerpt( $new_length = 20, $new_more = '...', $strip = false ) {
    // Excerpt length
    add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );
    function custom_excerpt_length( $new_length ) {
        return $new_length;
    }

    // Excerpt More
    add_filter( 'excerpt_more', 'custom_excerpt_more' );
    function custom_excerpt_more( $new_more ) {
        return $new_more;
    }

    // Output
    $output = get_the_excerpt();
    $output = apply_filters('wptexturize', $output);
    $output = apply_filters('convert_chars', $output);
    if(!$strip)
        $output = '<p>' . $output . '</p>';
    echo $output;
}
2
  • Thanks, that works. Why do you consider the 2nd method overkill - it looks like it's doing less to achieve the same thing.
    – Howdy_McGee
    Commented Nov 21, 2013 at 20:20
  • I'm not really a fan of nested function declarations. But if it works for you, it's a viable option. :) Commented Nov 21, 2013 at 20:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.