Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strip shortcode, keep content in between

The idea behind this question lies in getting excerpt of the posts created with avada but I can't strip the shortcodes from the post content to display the excerpt of the post.

Here is example of my post (using avada):

[fullwidth background_color="" background_image="" class="" id=""]
[one_full last="yes" spacing="yes" class="" id=""][fusion_text]
Content text ...   
[/fusion_text][/one_full][/fullwidth]`

The default the_excerpt(); doesn't work because of shortcodes. get_content() returns the full post content including shortcodes. Using strip_shortcodes() also removes the content between the shortcodes.

So my plan would be to strip shortcodes using pattern? and trim the message to mimic the excerpt functionality. PS: This pattern doesn't work.

like image 805
krizajb Avatar asked Oct 14 '15 19:10

krizajb


1 Answers

Use this regex:

$excerpt = get_the_excerpt();
$excerpt = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $excerpt);
like image 51
ThemesCreator Avatar answered Sep 22 '22 16:09

ThemesCreator