Doc WordPress WordPress Tricks, Tips & Snippets


Strip shortcodes but keep the content within

Sometimes you might need to strip enclosing shortcodes from some content for an excerpt or teaser output. of course WordPress has a useful function for this called strip_shortcodes(), but this function also strips the content in-between the enclosing shortcodes.

If you want to strip the enclosing shortcodes itself but keep the content in-between you can use a regex like this:


$content = '[someshortcode]example text[/someshortcode]';
$result = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $content);

echo $content; // example text


This would strip the shortcodes but leave the enclosed text unchanged.

Comments (0) Trackbacks (0)

No comments yet.

Leave a comment

No trackbacks yet.