Strip Short Codes From Excerpts
-
The plugin doesn’t seem to be stripping shortcodes from post excerpts properly.
Here’s a fix….
/** * @param $post_id * @param $excerpt_length * * @return mixed|string|string[]|null */ public static function elp_excerpt_by_id( $post_id, $excerpt_length ) { $the_post = get_post( $post_id ); $the_excerpt = $the_post->post_content; $the_excerpt = strip_tags( strip_shortcodes( $the_excerpt ) ); $the_excerpt = preg_replace( "~(?:\[/?)[^/\]]+/?\]~s", '', $the_excerpt ); $words = explode( ' ', $the_excerpt, $excerpt_length + 1 ); if ( count( $words ) > $excerpt_length ) { array_pop( $words ); array_push( $words, '...' ); $the_excerpt = implode( ' ', $words ); } $the_excerpt = nl2br( $the_excerpt ); $the_excerpt = str_replace( "<br>", " ", $the_excerpt ); $the_excerpt = str_replace( "<br />", " ", $the_excerpt ); $the_excerpt = str_replace( "\r\n", " ", $the_excerpt ); return $the_excerpt; }
- The topic ‘Strip Short Codes From Excerpts’ is closed to new replies.