Shorter way to strip all html, add shortcode, preg_repace

This works perfectly for what I’m using it for (auto meta description and og:description content for WordPress), but I wonder if there is a way to write it shorter/cleaner:

    $content = $post -> post_content;
    $content = wp_trim_words($content, 40, '...'); // 40 words
    $content = trim(str_replace(' ','',$content));
    $content = do_shortcode($content);   
    $content = html_entity_decode($content); 
    $content = strip_tags($content);    
    $content = preg_replace('/ss+/', '', $content);

Updated

Okay, I think this will do the trick. I wanted to be able to use it again and again and then I thought, hey maybe it’s like js. I’m a visual designer in my brain so it takes me sometime to get the hang of this stuff.

function do_meta_description_cab() {
        global $post;
        $content = $post -> post_content;
        $content = wp_trim_words($content, 40, '...'); // 40 words
        $content = trim(str_replace(' ','',$content));
        $content = do_shortcode($content);   
        $content = html_entity_decode($content); 
        $content = strip_tags($content);    
        $content = preg_replace('/ss+/', '', $content);

        return $content;

}

//Usage: $content = do_meta_description_cab();

Related posts

1 comment

  1. To strip all html opening and closing tags, you can use :

        $content = preg_replace('/<[^>]+>|</[^>]+>/', '', $content);
    

Comments are closed.