Skip to content

Instantly share code, notes, and snippets.

@dragunoff
Created December 12, 2012 08:02
Show Gist options
  • Save dragunoff/4265962 to your computer and use it in GitHub Desktop.
Save dragunoff/4265962 to your computer and use it in GitHub Desktop.
[WP] Generate an excerpt from any text
<?php
/**
* Generate an excerpt from any text.
*
* @author Ivaylo Draganov <[email protected]>
* @note A modified version of core WP function wp_trim_excerpt
* @param (string) $text - The text to trim.
* @param (int) $words - The number of words to trim.
* @return (string) The trimmed and filtered excerpt.
*/
function custom_trim_excerpt( $text = '', $words = '' ) {
$raw_excerpt = $text;
if ( $text ) {
$text = strip_shortcodes( $text );
$text = apply_filters('the_content', $text);
$text = str_replace(']]>', ']]&gt;', $text);
$excerpt_length = ( $words && is_numeric( $words ) ) ? absint( $words ) : apply_filters('excerpt_length', 55);
$excerpt_more = apply_filters('excerpt_more', ' ' . '[...]');
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
return apply_filters('wp_trim_excerpt', $text, $raw_excerpt);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment