Search code examples
wordpressshortcodestrip

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.


Solution

  • Use this regex:

    $excerpt = get_the_excerpt();
    $excerpt = preg_replace("~(?:\[/?)[^/\]]+/?\]~s", '', $excerpt);