Skip to content

Instantly share code, notes, and snippets.

@FerFuego
Last active February 5, 2021 18:32
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FerFuego/7e0c01c4b90c2b7cd65e3604e115e217 to your computer and use it in GitHub Desktop.
Save FerFuego/7e0c01c4b90c2b7cd65e3604e115e217 to your computer and use it in GitHub Desktop.
Trim text, strip shortcodes and excerpt return - Wordpress
/**
* Trim text, strip shortcodes and excerpt return
*
* @param Int $post - Post ID (optional)
* @param String $text - Text or get_the_conten() (optional)
* @param Int $words - Number of words to return
*
* @return string - "Ex: This is my text trim and..."
*
* Use: echo custom_trim_excerpt($post_id, '', 20 );
* Use: echo custom_trim_excerpt('', get_the_content(), 20 );
* Use: echo custom_trim_excerpt('', $my_text, 20 );
* Print: "This is my text trim and..."
*/
function custom_trim_excerpt ( $post = null, $text = null, $words = null ) {
if ( $post ) {
$content = get_the_content('', false, $post);
}
if ( $text ) {
$content = $text;
}
$content = excerpt_remove_blocks( $content );
$content = apply_filters( 'the_content', $content);
$content = strip_shortcodes( $content );
$content = str_replace( ']]>',']]>', $content);
if ($words) {
$content = wp_trim_words( $content, $words, '...' );
}
return $content;
}
@FacundoGamond
Copy link

very picante! thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment