I have this line of code displaying the excerpt in the theme:
<p class="desc"><?php echo mb_strimwidth(strip_tags(get_the_content('')), 0, 220, '...'); ?></p>
How do I put this code in to strip out the shortcodes from the excerpt?
$text = preg_replace( '|[(.+?)](.+?[/\1])?|s', '', $text);
I’m just getting into cutting up PHP so I’m needing just a little bit of help with this one.
Thanks!
Instead of re-inventing the wheel, I recommend you using the core WordPress function strip_shortcodes().
DONT USE REGEX in this case!
REGEX will remove your own notes from excerpts, for example:
Hello, on May 27 [1995] , blabla
so, better to use built-in function
strip_shortcodes
, which detects registered shortcodes and removes them:Posting a solution worked for me based on @T.Todua solution:
I split it into two functions since I also use the “more” filter as follow: