Replace shortcode in substring

I've created these two functions to toggle between the first paragraph of the content and then everything but the first paragraph so when the user selects 'More' it slides out the article smoothly without replacing the first paragraph that's already displayed as the excerpt.

However if there's a shortcode in that text it shows up as the shortcode syntax rather than displaying properly. I understand why it's not displaying but I'd like to replace any shortcodes in the text so just show up as text instead of shortcode syntax wrapped in brackets.

function get_first_paragraph(){
    global $post;

    $str = wpautop( str_replace( 'src="http://', 'src="https://', get_the_content() ));
    $str = substr( $str, 0, strpos( $str, '/p' ) + 4 );
    $str = strip_tags($str, 'astrongem');

    return 'p' . $str . '/p';
}
function get_the_post(){
    global $post;
    $str = wpautop( str_replace( 'src="http://', 'src="https://', get_the_content() ));
    $str = substr( $str, (strpos( $str, '/p')));
    return $str;
}

Topic query-string shortcode php customization Wordpress

Category Web


Check the $str with get_shortcode_regex(). If $match is returned, you need to parse $str with do_shortcode() and then return it, else false proceed.

  1. GetShortCodeRegEx
  2. DoShortCode

UPDATE

After Sleeping, and while researching to answer your comment, has_shortcode() should be replaced by get_shortcode_regex(), which I have done in my original above.

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.