Forums

[resolved] Remove shortcode from content (3 posts)

  1. EntarteteMuzak
    Member
    Posted 1 year ago #

    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: http://wordpress.org/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.

  2. EntarteteMuzak
    Member
    Posted 1 year ago #

    Can I use the strip shortcode function?

  3. EntarteteMuzak
    Member
    Posted 1 year ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic