• Resolved EntarteteMuzak

    (@entartetemuzak)


    I’m trying to remove all short code from post content when I display the post in a search result. As it were, I also use short code in excerpts. I’ve found this solution, but I can’t seem to make it work: https://www.ads-software.com/support/topic/remove-short-code-from-excerpt?replies=4#post-1924799

    Here is my code (the short code I’m trying to remove is [col-sect] and [column], as used by WP Columnize plugin)

    In functions:

    <?php
    add_filter( 'the_excerpt', 'remove_columns' );
    function remove_columns( $excerpt ) {
    	return preg_replace ('/\[column[^\]]*\](.*)\[\/column\]/', '$1', $excerpt);
    	return preg_replace ('/\[col-sect[^\]]*\](.*)\[\/col-sect\]/', '$1', $excerpt);
    }?>

    In page:
    <?php remove_filter('the_excerpt', 'do_shortcode') ; add_filter ('the_excerpt', 'remove_columns') ; the_excerpt(); ?>

    I’ve added a remove filter on do_shortcode to remove the short code function from the excerpt, but that still leaves [col-sect] and [column] in the text. Also it seems that the first and the last occurrence of the [column] is removed but not the rest, while both instances of [col-sect] stays intact.

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter EntarteteMuzak

    (@entartetemuzak)

    Can I use the strip shortcode function?

    Thread Starter EntarteteMuzak

    (@entartetemuzak)

    Fixed it with this:

    in Functions

    <?php
    add_filter( 'the_excerpt', 'remove_columns' );
    function remove_columns( $excerpt ) {
    	return preg_replace ("/\[(\S+)\]/e", "", $excerpt);
    }?>

    In template:
    <?php remove_all_shortcodes('the_excerpt') ; the_excerpt(); ?>

    The only thing that ticks me is that the […] after the excerpt disappears if I use this technique on the excerpt.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Remove shortcode from content’ is closed to new replies.