Strip HTML and Shortcodes from wordpress that uses Fusion Builder

First thing is i’m using WordPress > Avada Theme > and Fusion Core Plugin.

What i want to do is strip the content of any post down to a few characters. And what i have works with almost everything minus, post content that uses the Fusion Core plugin.

Read More

Here is my function that should get the excerpt and shrink it down.

function get_my_excerpt_by_id($post_id, $length, $strip_html)
{   
    if(!$length)        
      $excerpt_length = 35;     
    else        
      $excerpt_length = $length;

    if(!isset($strip_html) || $strip_html == true)      
      $strip_html = true;

    $the_post = get_post($post_id); //Gets post ID
    $the_excerpt = apply_filters('the_content',$the_post->post_content); //Gets post_content to be used as a basis for the excerpt

    if($strip_html == true)
        $the_excerpt = strip_tags(strip_shortcodes($the_excerpt)); //Strips tags and images

    $words = explode(' ', $the_excerpt, $excerpt_length + 1);

    if(count($words) > $excerpt_length) :
        array_pop($words);
        array_push($words, '…');
        $the_excerpt = implode(' ', $words);
    endif;

    return $the_excerpt; 
}

So this works like a charm EXCEPT if i’m using Fusion Core Plugin for one of the post types beccause then i get this

‘.fusion-fullwidth-4 { padding-left: 0px !important; …’

Any idea why its not stripping the shortcodes and css and html? Although in looking at it, maybe its just the css? that is staying. Thank you!

EDIT::
SO it turns out that its just the css that stays. So now i have to figure out how to remove them. Everything after a period….

Related posts

2 comments

  1. I just wanted to post my answer here in case anybody need it for later, Thank you Ghulam Ali, for the help with this answer.

    So it turns out that when using Fusion Core, it adds <style> tags, that when removed using strip_tags, would leave the css code nested inside of the tag.

    So, to fix this we first remove all tags, MINUS <style> using

    $the_excerpt = strip_tags(strip_shortcodes($the_excerpt), '<style>');

    THEN, we strip out the <style> tags and its content using this nifty regex. (that i did not write but don’t know how to give the right credit to the person that did).

    $the_excerpt = preg_replace("|<styleb[^>]*>(.*?)</style>|s", "", $the_excerpt);

    This is the full function i use to get the content and output an excerpt from it. I hope this comes in handy for somebody!

    function get_my_excerpt_by_id($post_id, $length, $strip_html){
        if(!$length)
            $excerpt_length = 35;
        else
            $excerpt_length = $length;
    
        if(!isset($strip_html) || $strip_html == true)
            $strip_html = true;
    
        $the_post = get_post($post_id); //Gets post ID
        $the_excerpt = apply_filters('the_content',$the_post->post_content);//$the_post->post_content); //Gets post_content to be used as a basis for the excerpt
    
        if($strip_html == true)
        {
            $the_excerpt = strip_tags(strip_shortcodes($the_excerpt), '<style>');
            $the_excerpt = preg_replace("|<styleb[^>]*>(.*?)</style>|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);
        endif;
    
        return $the_excerpt;
    }
    
  2. add style tag into strip tags

    $the_excerpt = strip_tags(strip_shortcodes($the_excerpt), '<style>'); //add tags like
    

    i hope this will fix.

Comments are closed.