[wp-hackers] Re: filter the_exceprt

Jared Bangs jaredbangs at gmail.com
Mon Jan 12 01:31:14 GMT 2009

If you're just trying to use '[Read More]' instead of '[...]', why not
just do something like:

function sp_excerpt($text)
    return str_replace('[...]', '[Read More]', $text);
add_filter('the_excerpt', 'sp_excerpt');

I noticed that you've changed to using the 'get_the_excerpt' filter
rather than 'the_excerpt', which I think is why your original version
wasn't working.

The wp_trim_excerpt function (assuming that's where you got this from)
is already attached and run during 'get_the_excerpt', so then during
'the_excerpt', $text will never be an empty string (because
wp_trim_excerpt already ran), so the first if() is false and it falls
through and returns the text unmodified.

Unless you need to do something other than that basic replacement, I'd
recommend something more simple like I've got above.

If you really want your routine to run *instead of* wp_trim_excerpt,
you should *unregister* that filter for 'get_the_excerpt' and register
your function in its place. But then you'd have to make sure to keep
all that extra code up to date as WP advances and potentially changes
the way it does wp_trim_excerpt, which is probably more of a pain than
it's worth.

- Jared

On Sun, Jan 11, 2009 at 5:07 PM, Daniel Cameron <dan at sproutventure.com> wrote:
>> I'm basically trying to get rid of that horrible [...] ellipses forces on
>> the_exceprt, so maybe you have another solution (without a plugin).
> I noticed I needed to set the priority. Here's what I'm working with now,
> although there's no output.
> add_filter('get_the_excerpt', 'sp_excerpt', 1, 2);
> function sp_excerpt($text) { // Fakes an excerpt if needed
> if ( '' == $text ) {
> $text = get_the_content('');
>  $text = strip_shortcodes( $text );
>  $text = apply_filters('the_content', $text);
> $text = str_replace(']]>', ']]&gt;', $text);
> $text = strip_tags($text);
> $excerpt_length = 55;
> $words = explode(' ', $text, $excerpt_length + 1);
> if (count($words) > $excerpt_length) {
> array_pop($words);
> array_push($words, '
> $text = implode(' ', $words);
> }
> }
> return $text;
> }
> _______________________________________________
> wp-hackers mailing list
> wp-hackers at lists.automattic.com
> http://lists.automattic.com/mailman/listinfo/wp-hackers

More information about the wp-hackers mailing list