Automatically Shorten the Manual Excerpt

When you are building or modifying a wordpress theme, you have the choice of either using ‘the_content()’ to show the full article, or ‘the_excerpt()’ to show a shorter version.
By default, the latter limits the output to the first 55 words of the post, and automatcally strips formatting and images.
To make things more flexible, the editor also allows you to add a manual excerpt that will have preference before the automatically generated one.
There is no default limit to the length of this manual excerpt, which might break some carefully designed templates.

To apply the excerpt length limit to all excerpts, a modification of a core wordpress function ‘wp_trim_excerpt()’ would be needed.
However, changing core files is not a good idea, as the changes will be lost after each update.

Luckily, the same result can be achieved by using a fillter hook on ‘get_the_excerpt’; the code for it is added to functions.php of the theme.

(updated to utilize the filters ‘excert_length’ and ‘excerpt_more’ – aug 2010)

function wp_trim_all_excerpt($text) {
// Creates an excerpt if needed; and shortens the manual excerpt as well
global $post;
  $raw_excerpt = $text;
  if ( '' == $text ) {
    $text = get_the_content('');
    $text = strip_shortcodes( $text );
    $text = apply_filters('the_content', $text);
    $text = str_replace(']]>', ']]>', $text);
  }

$text = strip_tags($text);
$excerpt_length = apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more ); //since wp3.3
/*$words = explode(' ', $text, $excerpt_length + 1);
  if (count($words)> $excerpt_length) {
    array_pop($words);
    $text = implode(' ', $words);
    $text = $text . $excerpt_more;
  } else {
    $text = implode(' ', $words);
  }
return $text;*/
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt); //since wp3.3
}

remove_filter('get_the_excerpt', 'wp_trim_excerpt');
add_filter('get_the_excerpt', 'wp_trim_all_excerpt');

edit: expanded to take remove shortcode from the excerpt; 12/07/2010
edit: adapted to wp3.3; 16.01.2012

thanks to michael.oeser for his well written article: Wie man WordPress Textauszüge mit the_excerpt individuell anpasst. It contains even more details on how to tweak the excerpt.